SQL语句高级(四)

一、多层嵌套子查询
查询ccc老师任课的学生成绩!
mysql> select * from teacher;
+-----+-------+-------+---------------------+------------+--------+
| tno | tname | tsex  | tbirthday           | prof       | depart |
+-----+-------+-------+---------------------+------------+--------+
| 804 | aaa   | man   | 1986-12-10 00:00:00 | fujiaoshou | cs     |
| 825 | ccc   | man   | 1966-10-04 00:00:00 | zhujiao    | cs     |
| 856 | bbb   | woman | 1945-02-09 00:00:00 | jiaoshou   | dianzi |
+-----+-------+-------+---------------------+------------+--------+
3 rows in set (0.00 sec)

mysql> select * from teacher where tname = 'ccc';
+-----+-------+------+---------------------+---------+--------+
| tno | tname | tsex | tbirthday           | prof    | depart |
+-----+-------+------+---------------------+---------+--------+
| 825 | ccc   | man  | 1966-10-04 00:00:00 | zhujiao | cs     |
+-----+-------+------+---------------------+---------+--------+
1 row in set (0.00 sec)

mysql> select tno from teacher where tname = 'ccc';
+-----+
| tno |
+-----+
| 825 |
+-----+
1 row in set (0.00 sec)

mysql> select * from course where tno = (select tno from teacher where tname = 'ccc');
+-------+----------+-----+
| cno   | cname    | tno |
+-------+----------+-----+
| 3-105 | csdaolun | 825 |
+-------+----------+-----+
1 row in set (0.07 sec)

mysql> select cno from course where tno = (select tno from teacher where tname = 'ccc');
+-------+
| cno   |
+-------+
| 3-105 |
+-------+
1 row in set (0.00 sec)

mysql> select * from score --多层嵌套子查询!
    -> where cno = (select cno from course 
    -> where tno = (select tno from teacher where tname = 'ccc'));
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 100 | 3-105 |    100 |
| 103 | 3-105 |     99 |
| 104 | 3-105 |     59 |
+-----+-------+--------+
3 rows in set (0.02 sec)

二、多表查询
查询选秀某课程的同学人数多于4人的教师姓名!
mysql> select tno from course 
    -> where cno = (select cno from score group by cno having count(*)>4);
+-----+
| tno |
+-----+
| 856 |
+-----+
1 row in set (0.00 sec)

mysql> select tname from teacher 
    -> where tno=(select tno from course 
    -> where cno = (select cno from score group by cno having count(*)>4));
+-------+
| tname |
+-------+
| bbb   |
+-------+
1 row in set (0.01 sec)

三、in表示或者关系!
查询95034班和95035班全体学生的记录!
mysql> select * from student;
+-----+--------+---------------------+-------+-------+
| sno | sname  | sbirthday           | class | ssex  |
+-----+--------+---------------------+-------+-------+
| 100 | Java   | 1977-09-01 00:00:00 | 95033 | man   |
| 101 | C      | 1975-08-21 00:00:00 | 95034 | woman |
| 102 | C++    | 1976-02-11 00:00:00 | 95035 | woman |
| 103 | C#     | 1974-12-01 00:00:00 | 95034 | woman |
| 104 | Python | 1977-10-11 00:00:00 | 95033 | man   |
| 105 | JS     | 1974-11-11 00:00:00 | 95033 | woman |
+-----+--------+---------------------+-------+-------+
6 rows in set (0.00 sec)

mysql> select * from student where class in ('95034','95035');
+-----+-------+---------------------+-------+-------+
| sno | sname | sbirthday           | class | ssex  |
+-----+-------+---------------------+-------+-------+
| 101 | C     | 1975-08-21 00:00:00 | 95034 | woman |
| 102 | C++   | 1976-02-11 00:00:00 | 95035 | woman |
| 103 | C#    | 1974-12-01 00:00:00 | 95034 | woman |
+-----+-------+---------------------+-------+-------+
3 rows in set (0.00 sec)



猜你喜欢

转载自blog.csdn.net/qq_37150711/article/details/87225885
今日推荐