Table of Contents
带IN关键字的子查询
带EXISTS关键字的子查询
带ANY关键字的子查询
带ALL关键字的子查询
UNION合并查询结果
Home Database Mysql Tutorial MySQL学习笔记16:子查询_MySQL

MySQL学习笔记16:子查询_MySQL

Jun 01, 2016 pm 01:37 PM
Keywords

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In-depth analysis of the role and usage of the static keyword in C language In-depth analysis of the role and usage of the static keyword in C language Feb 20, 2024 pm 04:30 PM

In-depth analysis of the role and usage of the static keyword in C language

The role and examples of var keyword in PHP The role and examples of var keyword in PHP Jun 28, 2023 pm 08:58 PM

The role and examples of var keyword in PHP

Is go a keyword in C language? Detailed analysis Is go a keyword in C language? Detailed analysis Mar 16, 2024 am 10:30 AM

Is go a keyword in C language? Detailed analysis

How many keywords are there in c language? How many keywords are there in c language? Nov 22, 2022 pm 03:39 PM

How many keywords are there in c language?

Complete list of go language keywords Complete list of go language keywords Apr 07, 2024 pm 02:15 PM

Complete list of go language keywords

Detailed explanation of the role and usage of the extends keyword in PHP Detailed explanation of the role and usage of the extends keyword in PHP Jun 28, 2023 pm 08:04 PM

Detailed explanation of the role and usage of the extends keyword in PHP

Is while a keyword in go language? Is while a keyword in go language? Jun 04, 2021 pm 05:01 PM

Is while a keyword in go language?

Several uses of the base keyword in C# Several uses of the base keyword in C# Mar 13, 2024 pm 03:28 PM

Several uses of the base keyword in C#

See all articles