SQL查询语句例子

SQL查询语句例子
SQL查询语句例子

数据表的查询(select)

select 字段列表[as 别名], * from 数据表名

[where 条件语句]

[group by 分组字段]

[order by 排序字段列表desc]

[LIMIT startrow,rownumber]

1、Select 字段列表From 数据表

例:①、select id,gsmc,add,tel from haf (* 表示数据表中所有字段)

②、select 单价,数量,单价*数量as 合计金额from haf (As 设置字段的别名)

2、Select …from …Where 筛选条件式

筛选条件式:①、字符串数据:select * from 成绩单Where 姓名='李明'

②、万用字符:select * from 成绩单Where 姓名like '李%'

select * from 成绩单Where 姓名like '%李%'

select * from 成绩单Where 姓名like '%李_'

③、特殊的条件式:

⑴= / > / < / <> / >= / <=

⑵AND(逻辑与) OR(逻辑或) NOT(逻辑非)

⑶Where 字段名称in(值一,值二)

⑷Where 字段名称Is Null / Where 字段名称Is Not Null

3、Select …from …group by 字段

SQL函数:

SELECT sex,count(id) as women from `user` group by 'sex';

函数名描述函数名描述

AVG平均值Count计数

MAX最大值MIN最小值

Sum求和

4、Select …from …Order by 字段列表desc(倒,如果直接写为顺序)

5、Select …from …LIMIT ".$start_rowno.",".($pagesize+1)

第二节SQL语句实例应用

数据库说明:

student(学生表):

stdid int(11) id号

son char(5) 学号

sname char(20) 姓名

ssex tinyint(1) 性别

sage char(3) 年龄

sdept char(20) 所在系

course(课程表):

couid int(11) id号

cno char(5) 课程号

cname char(20) 课程名

cpno char(6) 选修课号

ccredit char(50) 学分

sc(学生选课表):

scid int(11) id号

cno char(5) 课程号

grade float 成绩

sno char(5) 学号

单表查询:

一、选择表中的若干字段:

查询指定列:

1、查询全体学生的学号与姓名;

select son,sname from student

2、查询全体学生的姓名、学号、所在系;

select sname,son,sdept from student

3、查询全体学生的详细记录;

select * from student

查询经过计算的值:

4、查全体学生的姓名及其出生年份

select sname,year(now())-sage as '出生年份' from student

5、查询全体学生的姓名、出生年份和所有系,要求用大(小)写字母表示所有系名

select sname as '姓名','出生与',year(now())-sage as '出生年份',UPPER(sdept) as '系别' from student

select sname as '姓名','出生与',year(now())-sage as '出生年份',lower(sdept) as '系别' from student

二、选择表中的若干记录:

消除取值重复的行:

6、查询选修了课程的学生学号

select distinct sno from sc

查询满足条件的记录:

比较大小:

7、查询计算机全体学生的名单

select sname from student where sdept='cs'

8、查询所有年龄在20岁以下的学生姓名及其年龄

select sname,sage from student where sage<20

9、查询考试成绩小于90分的学生的学号

select distinct sno from sc where grade<90

确定范围:

10、查询年龄在18-20岁之间的学生的姓名、系别和年龄。

select sname,sdept,sage from student where sage between 18 and 20

11、查询年龄不在19-20岁之间的学生的姓名、系别和年龄。

select sname,sdept,sage from student where sage not between 19 and 20

确定集合:

12、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。

select sname,ssex from student where sdept in('is','ma','cs')

13、查询不是信息系(is)、数学系(ma)的学生的姓名、系别和年龄。

select sname,ssex from student where sdept not in('is','ma')

字符匹配(like '<匹配串>' %代表任意长度(长度可以为0)的字符串; _代表任意单个字符,汉字得用两个"__"):

14、查询学号为95001的学生的详细情况

select * from student where son like '95001'

15、查询所有姓名李的学生的姓名、学号和性别。

select sname,son,ssex from student where sname like '李%'

16、查询姓名是两个字学生的姓名、学号和性别。

select sname,son,ssex from student where sname like '____'

17、查询所有不姓李的学生姓名。

select sname from student where sname not like '李__'

涉及空值的查询:

18、某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩,查询缺少成绩的学生的学号和相应的课程号。

select sno,cno from sc where grade is null

19、查询所有有成绩的学生学号和课程号。

select sno,cno from sc where grade is not null

多重条件查询(and or):

20、查询计算机系年龄在20岁的学生姓名。

select sname from student where sdept='cs' and sage=20

21、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。

select sname,ssex from student where sdept='is' or sdept='ma' or sdept='cs'

三、对查询结果排序:

22、查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列。

select sno,grade from sc where cno='3' order by grade desc

23、查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

select * from student order by sdept,sage desc

四、使用集函数:

24、查询学生总人数。

select count(*) as '总人数' from student

25、查询选修了课程的学生人数。

select count(distinct sno) as '人数' from sc

26、计算1号课程的学生平均成绩

select format(avg(grade),2) as '平均成绩' from sc where cno='1'

27、查询选修1号课程的学生最高分数。

select max(grade) from sc where cno='1'

五、对查询结果分组:

28、求各个课程号及相应的选课人数。

select cno as '课程号',count(sno) as '人数' from sc group by cno

29、查询选修了3门以上课程的学生学号。

select sno from sc group by sno having count(*)>2

注:where 子句与having 短语的区别在于作用对象不同,where 子句作用于基本表或视图,从中选择满足条件的记录,having短语作用于组,从中选择满足条件的组。

多表查询

同时查询两个以上的表,称为连接查询。

等值连接:当连接运算符为=时,为等值连接。

1、查询每个学生及其选修课程的情况(等值连接)。

select student.*,sc.* from student,sc where student.son=sc.sno

自然连接:在等值连接中把目标列中重复的属性列去掉。

2、查询每个学生及其选修课程的情况(自然连接)。

select student.son,sname,ssex,sage,sdept,cno,grade from student,sc where student.son=sc.sno

自身连接:连接操作不仅可以在两个表之间进行,也可以是一个表与其自己进行连接。

3、查询每一门课的间接先修课。

select https://www.360docs.net/doc/5c13580978.html,o,b.cpno,https://www.360docs.net/doc/5c13580978.html,ame from course a,course b where a.cpno=https://www.360docs.net/doc/5c13580978.html,o

复合条件连接:

4、查询选修2号课程且成绩在90分以上的所有学生。

select a.son,sname from student a,sc b where a.son=b.sno and

https://www.360docs.net/doc/5c13580978.html,o='2' and b.grade>90

5、查询每个学生的学号、姓名、选修的课程名及成绩。

select a.son,sname,cname,grade from student a,sc b ,course c where a.son=b.sno and https://www.360docs.net/doc/5c13580978.html,o=https://www.360docs.net/doc/5c13580978.html,o

相关主题
相关文档
最新文档