码迷,mamicode.com
首页 > 其他好文 > 详细

【9】查询练习:NOT IN、ANY、ALL、UNION

时间:2020-04-12 11:01:59      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:esc   imei   strong   成绩   bsp   sql   并集   png   art   

1.NOT IN:

mysql> select * from teacher;
+---------+-------------+---------+---------------------+----------+------------+
| tea_num | tea_name    | tea_sex | tea_birth           | tea_prof | department |
+---------+-------------+---------+---------------------+----------+------------+
| 0435    | LiMei       | F       | 1983-02-24 00:00:00 | prof     | Computer   |
| 0436    | MaDi        | F       | 1984-01-23 00:00:00 | assist   | Bio        |
| 0437    | LiZhe       | F       | 1974-01-23 00:00:00 | prof     | Econ       |
| 0438    | ShaoGuoYing | F       | 1985-06-17 00:00:00 | prof     | Math       |
| 0439    | Susan       | F       | 1985-07-18 00:00:00 | assist   | Math       |
| 0440    | Mary        | F       | 1990-05-02 00:00:00 | lecturer | Econ       |
+---------+-------------+---------+---------------------+----------+------------+

查询Math与Econ不同职称的教师的tea_name与tea_prof:

Econ中的职称类型:

mysql> select tea_prof from teacher where department in (Econ);
+----------+
| tea_prof |
+----------+
| prof     |
| lecturer |
+----------+

Math中与它不同的:

mysql> select tea_name,tea_prof from teacher where department = Math and tea_prof not in (select tea_prof from teacher where department in (Econ));
+----------+----------+
| tea_name | tea_prof |
+----------+----------+
| Susan    | assist   |
+----------+----------+

同理:

mysql> select tea_name,tea_prof from teacher where department = Econ and tea_prof not in (select tea_prof from teacher where department in (Math));
+----------+----------+
| tea_name | tea_prof |
+----------+----------+
| Mary     | lecturer |
+----------+----------+

2条结果合在一起:union求并集

mysql> select tea_name,tea_prof from teacher where department = Econ and tea_prof not in (select tea_prof from teacher where department in (Math))
    -> union
    -> select tea_name,tea_prof from teacher where department = Math and tea_prof not in (select tea_prof from teacher where department in (Econ));
+----------+----------+
| tea_name | tea_prof |
+----------+----------+
| Mary     | lecturer |
| Susan    | assist   |
+----------+----------+

 3.查询课程号为2-271和1-245的成绩:

mysql> select * from score where cour_num in (2-271);
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11423   | 2-271    |     75 |
| 11425   | 2-271    |     89 |
| 11426   | 2-271    |     82 |
+---------+----------+--------+
mysql> select * from score where cour_num in (1-245);
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11423   | 1-245    |     84 |
| 11426   | 1-245    |     61 |
| 11427   | 1-245    |     78 |
+---------+----------+--------+

any:任意一个

技术图片

mysql> select * from score where cour_num=2-271
    -> and degree>any(select degree from score where cour_num=1-245);
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11423   | 2-271    |     75 |
| 11425   | 2-271    |     89 |
| 11426   | 2-271    |     82 |
+---------+----------+--------+

按成绩降序排列:

mysql> select * from score where cour_num=2-271
    -> and degree>any(select degree from score where cour_num=1-245)
    -> order by degree desc;
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11425   | 2-271    |     89 |
| 11426   | 2-271    |     82 |
| 11423   | 2-271    |     75 |
+---------+----------+--------+

4.all:所有

mysql> select * from score
    -> where cour_num=2-271
    -> and degree>all(select degree from score where cour_num=1-245);
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11425   | 2-271    |     89 |
+---------+----------+--------+

技术图片

 

【9】查询练习:NOT IN、ANY、ALL、UNION

标签:esc   imei   strong   成绩   bsp   sql   并集   png   art   

原文地址:https://www.cnblogs.com/direwolf22/p/11984248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!