SQL语句高级(五)——union、any、not in

一、子查询
查询出cs系教师所教课程的成绩表!
mysql> select * from teacher where depart = 'cs';
+-----+-------+------+---------------------+------------+--------+
| 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     |
+-----+-------+------+---------------------+------------+--------+
2 rows in set (0.12 sec)

mysql> select * from course where tno in (select tno from teacher where depart = 'cs');
+--------+----------+-----+
| cno    | cname    | tno |
+--------+----------+-----+
| 6-1166 | math     | 804 |
| 3-105  | csdaolun | 825 |
+--------+----------+-----+
2 rows in set (0.06 sec)

mysql> select * from score 
    -> where cno in (select cno from course 
    -> where tno in (select tno from teacher where depart = 'cs
'));
+-----+--------+--------+
| sno | cno    | degree |
+-----+--------+--------+
| 101 | 6-1166 |     59 |
| 103 | 6-1166 |     59 |
| 104 | 6-1166 |     89 |
| 105 | 6-1166 |     89 |
| 100 | 3-105  |    100 |
| 103 | 3-105  |     99 |
| 104 | 3-105  |     59 |
+-----+--------+--------+

二、union和notin的使用
查询cs和dianzi系不同职称的教师的tname和prof。
mysql> select * from teacher;
+-----+-------+-------+---------------------+------------+--------+
| tno | tname | tsex  | tbirthday           | prof       | depart |
+-----+-------+-------+---------------------+------------+--------+
| 804 | aaa   | man   | 1986-12-10 00:00:00 | fujiaoshou | cs     |
| 811 | ddd   | man   | 1984-05-05 00:00:00 | zhujiao    | dianzi |
| 825 | ccc   | man   | 1966-10-04 00:00:00 | zhujiao    | cs     |
| 856 | bbb   | woman | 1945-02-09 00:00:00 | jiaoshou   | dianzi |
+-----+-------+-------+---------------------+------------+--------+
4 rows in set (0.00 sec)

mysql> select * from teacher where depart='cs'
    -> and prof not in
    -> (select prof from teacher where depart='dianzi');
+-----+-------+------+---------------------+------------+--------+
| tno | tname | tsex | tbirthday           | prof       | depart |
+-----+-------+------+---------------------+------------+--------+
| 804 | aaa   | man  | 1986-12-10 00:00:00 | fujiaoshou | cs     |
+-----+-------+------+---------------------+------------+--------+
1 row in set (0.03 sec)

mysql> select * from teacher where depart='dianzi'
    -> and prof not in
    -> (select prof from teacher where depart='cs');
+-----+-------+-------+---------------------+----------+--------+
| tno | tname | tsex  | tbirthday           | prof     | depart |
+-----+-------+-------+---------------------+----------+--------+
| 856 | bbb   | woman | 1945-02-09 00:00:00 | jiaoshou | dianzi |
+-----+-------+-------+---------------------+----------+--------+
1 row in set (0.00 sec)

mysql> select * from teacher where depart='cs'
    -> and prof not in
    -> (select prof from teacher where depart='dianzi')
    -> union
    -> select * from teacher where depart='dianzi'
    -> and prof not in
    -> (select prof from teacher where depart='cs');
+-----+-------+-------+---------------------+------------+--------+
| tno | tname | tsex  | tbirthday           | prof       | depart |
+-----+-------+-------+---------------------+------------+--------+
| 804 | aaa   | man   | 1986-12-10 00:00:00 | fujiaoshou | cs     |
| 856 | bbb   | woman | 1945-02-09 00:00:00 | jiaoshou   | dianzi |
+-----+-------+-------+---------------------+------------+--------+

三、any表示至少一个desc降序
查询选修了编号为3-105课程且成绩至少高于选修编号为3-245的同学的cno、sno和degree,
并按degree从高到低次序排序。(这里“至少”的意思是只要高于一个就满足!)
mysql> select * from score where cno='3-105'
    -> and 
    -> degree > any(select degree from score where cno='3-245');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 100 | 3-105 |    100 |
| 103 | 3-105 |     99 |
+-----+-------+--------+
2 rows in set (0.00 sec)
再排下序:
mysql> select * from score where cno='3-105'
    -> and degree > any(select degree from score where cno='3-245')
    -> order by degree desc;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 100 | 3-105 |    100 |
| 103 | 3-105 |     99 |
+-----+-------+--------+








猜你喜欢

转载自blog.csdn.net/qq_37150711/article/details/87235357