- 数据表结构与内容
student:
+-----+-------+---------------------+-------+
| sno | sname | sbirthday | class |
+-----+-------+---------------------+-------+
| 101 | zeng | 1977-09-01 00:00:00 | 95033 |
| 102 | ming | 1975-09-01 00:00:00 | 95031 |
| 103 | wang | 1976-09-01 00:00:00 | 95033 |
| 104 | li | 1977-02-01 00:00:00 | 95033 |
| 105 | lu | 1972-02-09 00:00:00 | 95031 |
| 106 | jun | 1974-06-03 00:00:00 | 95031 |
+-----+-------+---------------------+-------+
teacher
+-----+-------+------+---------------------+-----------+----------+
| tno | tname | tsex | tbirthday | prof | depart |
+-----+-------+------+---------------------+-----------+----------+
| 804 | cheng | m | 1958-09-01 00:00:00 | lecturer | computer |
| 805 | xun | m | 1969-09-01 00:00:00 | lecturer | math |
| 806 | ping | w | 1972-09-01 00:00:00 | professor | computer |
| 807 | bing | w | 1977-08-16 00:00:00 | professor | math |
+-----+-------+------+---------------------+-----------+----------+
course:
+-------+----------+-----+
| cno | cname | tno |
+-------+----------+-----+
| 3-105 | computer | 804 |
| 3-107 | computer | 806 |
| 4-105 | math | 805 |
| 4-115 | math | 807 |
+-------+----------+-----+
score:
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 101 | 3-105 | 86 |
| 102 | 4-105 | 96 |
| 103 | 3-105 | 89 |
| 104 | 3-105 | 66 |
| 105 | 3-107 | 81 |
| 106 | 4-115 | 76 |
+-----+-------+--------+
grade(等级表):
mysql> create table grade(
-> low int(3),
-> upp int(3),
-> grade char(1)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> insert into grade values(90,100,'A');
mysql> insert into grade values(80,89,'B');
mysql> insert into grade values(70,79,'C');
mysql> insert into grade values(60,69,'D');
mysql> insert into grade values(0,59,'E');
mysql> select * from grade;
+------+------+-------+
| low | upp | grade |
+------+------+-------+
| 90 | 100 | A |
| 80 | 89 | B |
| 70 | 79 | C |
| 60 | 69 | D |
| 0 | 59 | E |
+------+------+-------+
5 rows in set (0.00 sec)
- 查询练习
2.1 查询选修编号为"3-105"课程且成绩至少高于选修编号为"4-115"的同学的cno,sno和degree,并且按degree从高到低次序排序(至少高于对方就代表大于对方中至少一个同学的成绩)
any:any关键词可以理解为对于子查询返回的列中的任一数值
mysql> select * from score where cno='3-105' and degree>any(select degree from score where cno='4-115')
-> order by degree desc;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 89 |
| 101 | 3-105 | 86 |
+-----+-------+--------+
2 rows in set (0.36 sec)
2.2 查询选修编号为"3-105"且成绩高于选修编号为"4-115"课程的同学的cno,sno和degree
all:表示所有的意思
mysql> select * from score where cno='3-105' and degree>all(select degree from score where cno='4-115');
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 101 | 3-105 | 86 |
| 103 | 3-105 | 89 |
+-----+-------+--------+
2 rows in set (0.00 sec)
2.3 查询所有教师和同学的name和birthday(as取别名)
mysql> select sname as name,sbirthday as birthday from student
-> union select tname,tbirthday from teacher;
+-------+---------------------+
| name | birthday |
+-------+---------------------+
| zeng | 1977-09-01 00:00:00 |
| ming | 1975-09-01 00:00:00 |
| wang | 1976-09-01 00:00:00 |
| li | 1977-02-01 00:00:00 |
| lu | 1972-02-09 00:00:00 |
| jun | 1974-06-03 00:00:00 |
| cheng | 1958-09-01 00:00:00 |
| xun | 1969-09-01 00:00:00 |
| ping | 1972-09-01 00:00:00 |
| bing | 1977-08-16 00:00:00 |
+-------+---------------------+
10 rows in set (1.06 sec)
2.4 查询所有女教师和所有同学的name和birthday
mysql> select sname as name,sbirthday as birthday from student
-> union select tname,tbirthday from teacher where tsex='w';
+------+---------------------+
| name | birthday |
+------+---------------------+
| zeng | 1977-09-01 00:00:00 |
| ming | 1975-09-01 00:00:00 |
| wang | 1976-09-01 00:00:00 |
| li | 1977-02-01 00:00:00 |
| lu | 1972-02-09 00:00:00 |
| jun | 1974-06-03 00:00:00 |
| ping | 1972-09-01 00:00:00 |
| bing | 1977-08-16 00:00:00 |
+------+---------------------+
8 rows in set (0.00 sec)
2.5 查询成绩比该课程平均成绩低的同学的成绩表
mysql> select * from score as a where degree<(select avg(degree) from score as b where a.cno=b.cno);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 104 | 3-105 | 66 |
+-----+-------+--------+
1 row in set (0.00 sec)
mysql> select * from score as a where degree<(select avg(degree) from score as b group by cno having a.cno=b.cno);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 104 | 3-105 | 66 |
+-----+-------+--------+
1 row in set (0.00 sec)
2.6 查询所有任课(有课程安排)教师的tname和depart
mysql> select tno,tname,depart from teacher where tno in (select tno from course);
+-----+-------+----------+
| tno | tname | depart |
+-----+-------+----------+
| 804 | cheng | computer |
| 805 | xun | math |
| 806 | ping | computer |
| 807 | bing | math |
+-----+-------+----------+
4 rows in set (0.00 sec)
2.7 查询至少有1名男老师的部门
mysql> select tno,tsex,depart from teacher where tsex='m' group by depart having count(*)>=1;
+-----+------+----------+
| tno | tsex | depart |
+-----+------+----------+
| 804 | m | computer |
| 805 | m | math |
+-----+------+----------+
2 rows in set (0.00 sec)
2.8 查询student表中名字字母不包括i的同学记录
mysql> select * from student where sname not like '%i%';
+-----+-------+---------------------+-------+
| sno | sname | sbirthday | class |
+-----+-------+---------------------+-------+
| 101 | zeng | 1977-09-01 00:00:00 | 95033 |
| 103 | wang | 1976-09-01 00:00:00 | 95033 |
| 105 | lu | 1972-02-09 00:00:00 | 95031 |
| 106 | jun | 1974-06-03 00:00:00 | 95031 |
+-----+-------+---------------------+-------+
4 rows in set (0.00 sec)
2.9 查询student表中每个学生的姓名和年龄
mysql> select sname,year(now())-year(sbirthday) as age from student;
+-------+------+
| sname | age |
+-------+------+
| zeng | 43 |
| ming | 45 |
| wang | 44 |
| li | 43 |
| lu | 48 |
| jun | 46 |
+-------+------+
6 rows in set (0.00 sec)
2.10 查询student表中最大和最小的birthday日期值
mysql> select max(sbirthday) as max,min(sbirthday) as min from student;
+---------------------+---------------------+
| max | min |
+---------------------+---------------------+
| 1977-09-01 00:00:00 | 1972-02-09 00:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)
2.11 以班号和年龄从大到小的顺序查询student表中的全部记录(出生越早年龄越大)
mysql> select * from student order by class desc,sbirthday;
+-----+-------+---------------------+-------+
| sno | sname | sbirthday | class |
+-----+-------+---------------------+-------+
| 103 | wang | 1976-09-01 00:00:00 | 95033 |
| 104 | li | 1977-02-01 00:00:00 | 95033 |
| 101 | zeng | 1977-09-01 00:00:00 | 95033 |
| 105 | lu | 1972-02-09 00:00:00 | 95031 |
| 106 | jun | 1974-06-03 00:00:00 | 95031 |
| 102 | ming | 1975-09-01 00:00:00 | 95031 |
+-----+-------+---------------------+-------+
6 rows in set (0.00 sec)
2.12 查询男教师及所上课程
mysql> select cno,cname from course where tno in (select tno from teacher where tsex='m');
+-------+----------+
| cno | cname |
+-------+----------+
| 3-105 | computer |
| 4-105 | math |
+-------+----------+
2 rows in set (0.00 sec)
2.13 查询最高分同学的sno,cno和degree列
mysql> select * from score where degree=(select max(degree) from score);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 102 | 4-105 | 96 |
+-----+-------+--------+
1 row in set (0.00 sec)
2.14 查询和"cheng"同性别的所有教师的tname
mysql> select tname from teacher where tsex=(select tsex from teacher where tname='cheng');
+-------+
| tname |
+-------+
| cheng |
| xun |
+-------+
2 rows in set (0.01 sec)
2.15 查询和"cheng"同性别并且同职位的所有教师的tname
mysql> select tname from teacher where tsex=(select tsex from teacher where tname='cheng')
-> and prof=(select prof from teacher where tname='cheng');
+-------+
| tname |
+-------+
| cheng |
| xun |
+-------+
2 rows in set (0.00 sec)
2.16 查询所有选修cname为computer的男同学的成绩表
student表新增一列ssex,并设置默认值
mysql> alter table student add column ssex varchar(5);
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> update student set ssex='m';
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0
mysql> select * from score where cno in (select cno from course where cname='computer')
-> and sno in (select sno from student where ssex='m');
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 101 | 3-105 | 86 |
| 103 | 3-105 | 89 |
| 104 | 3-105 | 66 |
| 105 | 3-107 | 81 |
+-----+-------+--------+
4 rows in set (0.00 sec)
2.17 查询所有同学的sno,cno和grade列
mysql> select sno,cno,grade,degree from score,grade where degree between low and upp;
+-----+-------+-------+--------+
| sno | cno | grade | degree |
+-----+-------+-------+--------+
| 101 | 3-105 | B | 86 |
| 102 | 4-105 | A | 96 |
| 103 | 3-105 | B | 89 |
| 104 | 3-105 | D | 66 |
| 105 | 3-107 | B | 81 |
| 106 | 4-115 | C | 76 |
+-----+-------+-------+--------+
6 rows in set (0.00 sec)