一文简单了解MySQL子查询,至少可以玩得转了。
什么是子查询
笔者简单脑补了一下,所谓的子查询其实就是指将一个查询语句嵌套在另一个查询语句中,子查询使SQL语句更容易编写,通过子查询可以实现多表查询哦!
关于它的语法
-
子查询在 WHERE 中的语法格式如下:
WHERE <表达式> <操作符> (子查询)
-
注意,操作符可以是比较运算符和 IN、NOT IN、EXISTS、NOT EXISTS等。
-
IN | NOT IN
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE;若使用关键字 NOT,则返回值正好相反。
-
EXISTS | NOT EXISTS
用于判断子查询的结果集是否为空,若子查询的结果集不为空,返回 TRUE,否则返回 FALSE;若使用关键字 NOT,则返回的值正好相反。
实战案例
1. 操作符:“IN”案例
mysql> select ip from zabbix.interface where hostid in (select hostid from zabbix.hosts where name='zbxproxy03');
+----------------+
| ip |
+----------------+
| 192.168.11.157 |
+----------------+
1 row in set (0.00 sec)
外层的 SELECT 查询称为父查询,圆括号中嵌入的查询称为子查询(子查询必须放在圆括号内)。MySQL在处理上例的 SELECT 语句时,执行流程为:先执行子查询,再执行父查询。
手动步骤分解进行剖析
# 先执行子查询,查询主机zbxproxy03的hostid
mysql> select hostid from zabbix.hosts where name='zbxproxy03';
+--------+
| hostid |
+--------+
| 10360 |
+--------+
1 row in set (0.00 sec)
# 再执行父查询,匹配上一步查询到的hostid
mysql> select ip from zabbix.interface where hostid in (10360);
+----------------+
| ip |
+----------------+
| 192.168.11.157 |
+----------------+
1 row in set (0.00 sec)
2. 操作符:“NOT IN”案例
mysql> select name from zabbix.hosts where status=0 and name not like '%{%' and hostid not in (select hostid from zabbix.interface where ip='192.168.11.156');
+--------------+
| name |
+--------------+
| zbxser01 |
| mysql-master |
| mysql-db02 |
| zbxser02 |
| zbxproxy01 |
| zbxproxy04 |
| zbxproxy03 |
+--------------+
3. 操作符:“=”案例
案例:查询ip:192.168.11.158对应的主机名称
mysql> select name from hosts where hostid=(select hostid from interface where ip='192.168.11.158');
+------------+
| name |
+------------+
| zbxproxy04 |
+------------+
1 row in set (0.00 sec)
mysql> select ip from interface where hostid=(select hostid from hosts where name='zbxproxy01');
+----------------+
| ip |
+----------------+
| 192.168.11.155 |
+----------------+
1 row in set (0.00 sec)
4. 操作符:“<>”案例
案例:查询IP不等于192.168.11.156、并且只查询状态只为0、名称不包含“{”的主机名称
mysql> select name from hosts where status=0 and name not like '%{%' and hostid<>(select hostid from interface where ip='192.168.11.156');
+--------------+
| name |
+--------------+
| zbxser01 |
| mysql-master |
| mysql-db02 |
| zbxser02 |
| zbxproxy01 |
| zbxproxy04 |
| zbxproxy03 |
+--------------+
7 rows in set (0.00 sec)
5. 操作符:“EXISTS”案例
案例:查询 hosts 表中是否存在 status=0 的状态的主机,如果存在,就查询出 users 表中的记录
mysql> select alias,name from users where exists(select name from hosts where status=0);
+-------+--------+
| alias | name |
+-------+--------+
| Admin | Zabbix |
| guest | |
+-------+--------+
2 rows in set (0.00 sec)
案例:查询hosts表中的status字段是否存在0的值,如果有就通过右连接查询hosts和interface
mysql> select h.name,i.ip from hosts h right join interface i on i.ip='192.168.11.156' and status=0 where exists(select name from hosts where status=0);
+--------------+----------------+
| name | ip |
+--------------+----------------+
| NULL | 192.168.11.151 |
| NULL | 192.168.11.152 |
| NULL | 192.168.11.153 |
| NULL | 192.168.11.154 |
| NULL | 192.168.11.155 |
| zbxser01 | 192.168.11.156 |
| {#HV.NAME} | 192.168.11.156 |
| {#VM.NAME} | 192.168.11.156 |
| mysql-master | 192.168.11.156 |
| mysql-db02 | 192.168.11.156 |
| zbxser02 | 192.168.11.156 |
| zbxproxy01 | 192.168.11.156 |
| zbxproxy02 | 192.168.11.156 |
| zbxproxy04 | 192.168.11.156 |
| zbxproxy03 | 192.168.11.156 |
| NULL | 192.168.11.157 |
| NULL | 192.168.11.158 |
+--------------+----------------+
17 rows in set (0.00 sec)
6. 操作符:“NOT EXISTS”案例
案例:查询hosts表的status没有9的值的话,就执行查询users表
mysql> select alias,name from users where not exists(select name from hosts where status=9);
+-------+--------+
| alias | name |
+-------+--------+
| Admin | Zabbix |
| guest | |
+-------+--------+
2 rows in set (0.00 sec)
至此,笔者今晚时间有限,关于子查询的简单剖析就此搁笔,关于更深入的剖析以后继续,希望大家多多关注、支持、点赞、转发。