Home > Database > Mysql Tutorial > MySQL学习笔记16:子查询_MySQL

MySQL学习笔记16:子查询_MySQL

WBOY
Release: 2016-06-01 13:37:20
Original
1070 people have browsed it

bitsCN.com

子查询是将一个查询语句嵌套在另一个查询语句中

内层查询语句的查询结果,可以为外层查询语句提供查询条件

因为在特定情况下,一个查询语句的条件需要另一个查询语句来获取

参考表:employee

 

参考表:department

 

 

带IN关键字的子查询
mysql> SELECT * FROM employee    -> WHERE d_id IN    -> (SELECT d_id FROM department);+------+------+--------+------+------+--------------------+| num  | d_id | name   | age  | sex  | homeaddr           |+------+------+--------+------+------+--------------------+|    1 | 1001 | 张三   |   26 | 男   | 北京市海淀区         ||    2 | 1001 | 李四   |   24 | 女   | 北京市昌平区         ||    3 | 1002 | 王五   |   25 | 男   | 湖南长沙市           |+------+------+--------+------+------+--------------------+3 rows in set (0.00 sec)
Copy after login

此处首先查询出department表中所有d_id字段的信息,并将结果作为条件

接着查询employee表中以d_id为条件的所有字段信息

NOT IN的效果与上面刚好相反

 

带比较运算符的子查询

mysql> SELECT d_id, d_name FROM department    -> WHERE d_id!=    -> (SELECT d_id FROM employee WHERE age=24);+------+-----------+| d_id | d_name    |+------+-----------+| 1002 | 生产部    || 1003 | 销售部    |+------+-----------+2 rows in set (0.00 sec)
Copy after login

这里查询出了哪些部门没有年龄为24岁的员工,看起来有点复杂

此外,运算符还有很多,这里不再赘述

 

带EXISTS关键字的子查询

EXISTS关键字表示存在。使用EXISTS关键字时,内层查询语句不返回查询的记录,而是返回一个真假值,如果内层查询语句查询到满足条件的记录,就返回true,否则返回false

当返回的值为true时,外层查询语句将进行查询,否则不进行查询

mysql> SELECT * FROM employee    -> WHERE EXISTS    -> (SELECT d_name FROM department WHERE d_id=1004);Empty set (0.00 sec)
Copy after login

此处内层循环并没有查询到满足条件的结果,因此返回false,外层查询不执行

NOT EXISTS刚好与之相反

当然,EXISTS关键字可以与其他的查询条件一起使用

条件表达式与EXISTS关键字之间用AND或者OR来连接

mysql> SELECT * FROM employee    -> WHERE age>24 AND EXISTS    -> (SELECT d_name FROM department WHERE d_id=1003);+------+------+--------+------+------+--------------------+| num  | d_id | name   | age  | sex  | homeaddr           |+------+------+--------+------+------+--------------------+|    1 | 1001 | 张三   |   26 | 男   | 北京市海淀区         ||    3 | 1002 | 王五   |   25 | 男   | 湖南长沙市           |+------+------+--------+------+------+--------------------+2 rows in set (0.00 sec)
Copy after login

 

带ANY关键字的子查询

 ANY关键字表示满足其中任一条件

mysql> SELECT * FROM employee    -> WHERE d_id!=ANY    -> (SELECT d_id FROM department);+------+------+--------+------+------+--------------------+| num  | d_id | name   | age  | sex  | homeaddr           |+------+------+--------+------+------+--------------------+|    1 | 1001 | 张三   |   26 | 男   | 北京市海淀区         ||    2 | 1001 | 李四   |   24 | 女   | 北京市昌平区         ||    3 | 1002 | 王五   |   25 | 男   | 湖南长沙市           ||    4 | 1004 | Aric   |   15 | 男   | England            |+------+------+--------+------+------+--------------------+4 rows in set (0.00 sec)
Copy after login

 

带ALL关键字的子查询

ALL关键字表示满足其中所有条件

mysql> SELECT * FROM employee    -> WHERE d_id>=ALL    -> (SELECT d_id FROM department);+------+------+------+------+------+----------+| num  | d_id | name | age  | sex  | homeaddr |+------+------+------+------+------+----------+|    4 | 1004 | Aric |   15 | 男   | England  |+------+------+------+------+------+----------+1 row in set (0.00 sec)
Copy after login

暂时不明白这两条语句是什么意思,到时候再补上

 

UNION合并查询结果
mysql> SELECT d_id FROM employee    -> UNION    -> SELECT d_id FROM department;+------+| d_id |+------+| 1001 || 1002 || 1004 || 1003 |+------+4 rows in set (0.00 sec)
Copy after login

合并比较好理解,也就是将多个查询的结果合并在一起,然后去除其中的重复记录

如果想保存重复记录可以使用UNION ALL语句

 

 

 

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template