zoukankan      html  css  js  c++  java
  • 【9】查询练习:NOT IN、ANY、ALL、UNION

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

  • 相关阅读:
    mysql主从部署
    解决mysql登录警告问题
    使用selenium控制已打开的浏览器
    jTopo生成网络拓扑图
    Python模拟登陆正方教务系统并抓取成绩单
    Pandas库学习笔记
    三种方法实现统计一个句子中的字母数 (setdefault、defaultdict的使用)
    numpy学习笔记
    字符串的模式匹配——Brute-Force算法和KMP算法
    用生成器来改写直接返回列表的函数
  • 原文地址:https://www.cnblogs.com/direwolf22/p/11984248.html
Copyright © 2011-2022 走看看