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

  • 相关阅读:
    nodejs中的全局函数setTimeout/clearTimeout,setInterval/clearInterval,unref/ref
    nodejs的核心对象console
    创建一个服务器,解析当前的url并根据url并作出相应的响应
    nodejs创建服务并加载一个html文件
    nodejs读文件
    Get和Post的区别
    ui-grid 网格布局--jQueryMobile
    web开发常见问题
    全选和全不选
    微信小程序-调用工具js文件/utils文件中的函数/变量
  • 原文地址:https://www.cnblogs.com/direwolf22/p/11984248.html
Copyright © 2011-2022 走看看