查询练习:NOT INANYALLUNION

Posted direwolf22

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询练习:NOT INANYALLUNION相关的知识,希望对你有一定的参考价值。

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 |
+---------+----------+--------+

技术图片

 

以上是关于查询练习:NOT INANYALLUNION的主要内容,如果未能解决你的问题,请参考以下文章

简单查询练习

练习-----查询

MySQL查询练习

SQL查询练习

MySql练习---[SQL查询语句练习]

数据库-mysql查询练习