整理各种查询语句,需要查询数据请参考:
https://blog.csdn.net/chennuan1991/article/details/105240457
基础查询语句,请参考:
https://blog.csdn.net/chennuan1991/article/details/105250429
https://blog.csdn.net/chennuan1991/article/details/105255954
多层嵌套查询
查询‘李想’教师任课的学生成绩
步骤1:查询李想的教师编号
mysql> select tno from teacher where tname ='李想';
+-----+
| tno |
+-----+
| 804 |
+-----+
1 row in set (0.00 sec)
步骤2:根据教师编号tno查询出所教课程的编号cno
mysql> select cno from course where tno =(select tno from teacher where tname ='李想');
+-------+
| cno |
+-------+
| 3-245 |
+-------+
1 row in set (0.00 sec)
步骤3:根据tno,cno查询学生成绩
mysql> select * from score where cno = (select cno from course where tno =(select tno from teacher where tname ='李想'));
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
+-----+-------+--------+
3 rows in set (0.00 sec)
函数
year()函数
查询和学号为108、101的同学同年出生的所有学生的sno,sname,birthday列
步骤1:查询108、101同学的出生年份
mysql> select year(birthday) from students where sno in(108,101);
+----------------+
| year(birthday) |
+----------------+
| 1977 |
| 1975 |
+----------------+
2 rows in set (0.00 sec)
步骤2:查询同年学生的信息
mysql> select sno,sname,birthday from students where year(birthday) in(select year(birthday) from students where sno in(108,101));
+-----+--------+---------------------+
| sno | sname | birthday |
+-----+--------+---------------------+
| 101 | 张三 | 1977-09-01 00:00:00 |
| 102 | 李四 | 1975-10-02 00:00:00 |
| 105 | 小丽 | 1975-02-10 00:00:00 |
| 108 | 大妞 | 1975-02-10 00:00:00 |
+-----+--------+---------------------+
4 rows in set (0.00 sec)
count()函数
查询95031班学生人数
mysql> select count(*) from students where class = '95031';
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
一、count情况
1、count(1):可以统计表中所有数据,不统计所有的列,用1代表代码行,在统计结果中包含列字段为null的数据;
2、count(字段):只包含列名的列,统计表中出现该字段的次数,并且不统计字段为null的情况;
3、count(*):统计所有的列,相当于行数,统计结果中会包含字段值为null的列;
二、count执行效率
列名为主键,count(列名)比count(1)快;列名不为主键,count(1)会比count(列名)快;
如果表中多个列并且没有主键,则count(1)的执行效率优于count(*);
如果有主键,则select count(主键)的执行效率是最优的;如果表中只有一个字段,则select count(*)最优。
any()
查询选修编号为3-105 课程,且成绩至少高于选修编号为3-245的同学的cno,sno,degree,成绩由高到低排序
步骤1:查询3-245课程的成绩
mysql> select degree from score where cno = '3-245';
+--------+
| degree |
+--------+
| 86 |
| 75 |
| 68 |
+--------+
3 rows in set (0.00 sec)
步骤2:查询选修编号为3-105 课程,且成绩至少高于选修编号为3-245的同学的cno,sno,degree,成绩由高到低排序
mysql> select cno,sno,degree from score where cno ='3-105' and degree >any(select degree from score where cno = '3-245') order by degree desc;
+-------+-----+--------+
| cno | sno | degree |
+-------+-----+--------+
| 3-105 | 103 | 92 |
| 3-105 | 105 | 88 |
| 3-105 | 109 | 76 |
+-------+-----+--------+
3 rows in set (0.00 sec)
max()
查询score表中最高分的学生学号和课程号
mysql> select sno,cno,degree from score where degree = (select max(degree) from score);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
+-----+-------+--------+
1 row in set (0.00 sec)
min()
查询score表中最低分的学生学号和课程号
mysql> select sno,cno,degree from score where degree = (select min(degree) from score);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 109 | 3-245 | 68 |
+-----+-------+--------+
1 row in set (0.00 sec)
avg()
查询每门课程的平均成绩
mysql> select avg(degree) from score group by cno;
+-------------+
| avg(degree) |
+-------------+
| 85.3333 |
| 76.3333 |
| 81.0000 |
+-------------+
3 rows in set (0.00 sec)
取别名
查询所有教师和同学的name,sex,birthday
mysql> select sname as name,ssex as sex,birthday as birthday1 from students
-> union
-> select tname,tsex,tbirthday from teacher;
+--------+-----+---------------------+
| name | sex | birthday1 |
+--------+-----+---------------------+
| 张三 | 男 | 1977-09-01 00:00:00 |
| 李四 | 男 | 1975-10-02 00:00:00 |
| 王五 | 女 | 1976-01-23 00:00:00 |
| 铁柱 | 男 | 1976-02-20 00:00:00 |
| 小丽 | 女 | 1975-02-10 00:00:00 |
| 小红 | 男 | 1974-06-03 00:00:00 |
| 小绿 | 男 | 1976-02-20 00:00:00 |
| 大妞 | 女 | 1975-02-10 00:00:00 |
| 乖乖 | 男 | 1974-06-03 00:00:00 |
| 李想 | 男 | 1958-06-03 00:00:00 |
| 王超 | 女 | 1970-02-10 00:00:00 |
| 刘能 | 女 | 1961-06-03 00:00:00 |
| 张丹 | 男 | 1969-02-20 00:00:00 |
+--------+-----+---------------------+
13 rows in set (0.03 sec)
union和not in()
查询计算机系与电子工程系不同职称的教师的tname,prof
mysql> select tname,prof from teacher where tdepar='电子工程系' and prof not in(select prof from teacher where tdepar='计算机系')
-> union
-> select tname,prof from teacher where tdepar='计算机系' and prof not in(select prof from teacher where tdepar='电子工程系');
+--------+-----------+
| tname | prof |
+--------+-----------+
| 张丹 | 讲师 |
| 李想 | 副教授 |
+--------+-----------+
2 rows in set (0.00 sec)
参考
感谢code 158编程俱乐部