mysql> select * from t2;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | Mark | 29 |
| 2 | Frank | 32 |
| 3 | Niko | 27 |
+------+-------+------+
3 rows in set (0.00 sec)
mysql> select * from t1;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | Tube | 31 |
| 2 | Kevin | 34 |
| 3 | Todd | 32 |
+------+-------+------+
3 rows in set (0.00 sec)
mysql> select * from t2 where age >any (select age from t1);
+------+-------+------+
| id | name | age |
+------+-------+------+
| 2 | Frank | 32 |
+------+-------+------+
1 row in set (0.07 sec)
mysql> select * from log;
+------+----------+------------------------------------------------+
| id | category | log |
+------+----------+------------------------------------------------+
| 1 | Nginx | upstream timed out(110: Connection timed out) |
| 2 | Apache | File does notexist:/var/www/html/1.html |
| 3 | Apache | Could not openfile:/var/images/banner.gif |
| 4 | MySQL | InnoDB: Unable tolock ./ibdata1, error: 11 |
+------+----------+------------------------------------------------+
4 rows in set (0.05 sec)
mysql>
mysql>
mysql> select * from state;
+---------+-------+---------+---------+
| apache | Nginx | App | MySQL |
+---------+-------+---------+---------+
| Succeed | Fail | Succeed | Succeed |
+---------+-------+---------+---------+
1 row in set (0.09 sec)
mysql> select * from log where category= 'Nginx' and exists (select * from state where Nginx='Fail');
+------+----------+------------------------------------------------+
| id | category | log |
+------+----------+------------------------------------------------+
| 1 | Nginx | upstream timed out(110: Connection timed out) |
+------+----------+------------------------------------------------+
1 row in set (0.00 sec)
如果查询结果为真,显示;为假不显示。
mysql> select * from log where category= 'Apache';
+------+----------+--------------------------------------------+
| id | category | log |
+------+----------+--------------------------------------------+
| 2 | Apache | File does notexist:/var/www/html/1.html |
| 3 | Apache | Could not openfile:/var/images/banner.gif |
+------+----------+--------------------------------------------+
2 rows in set (0.00 sec)
mysql> select * from log where category= 'Apache' and exists (select * from state where apache='Fail');
Empty set (0.00 sec)
IN子查询
IN关键字对子查询结果进行运算,并判断是否返回行。如果有则执行外部查询语句。如果没有则不执行。
外查询返回的结果匹配子查询的结果。
mysql> select * from People;
+------+------+-----------+-------------+
| name | age | certnum | phone |
+------+------+-----------+-------------+
| Lee | 20 | 123456789 | 13021981234 |
| John | 32 | 987654321 | 13998763456 |
| Lucy | 45 | 678954321 | 13098784321 |
| Ken | 33 | 986745321 | 18609073544 |
+------+------+-----------+-------------+
4 rows in set (0.03 sec)
mysql> select * from Blacklist;
+------+------+-------+--------+
| ID | Name | Price | Level |
+------+------+-------+--------+
| 1 | John | 3000 | normal |
| 2 | Lucy | 24000 | hard |
+------+------+-------+--------+
2 rows in set (0.08 sec)
mysql> select * from People where nameIN
-> (select Name from Blacklist);
+------+------+-----------+-------------+
| name | age | certnum | phone |
+------+------+-----------+-------------+
| John | 32 | 987654321 | 13998763456 |
| Lucy | 45 | 678954321 | 13098784321 |
+------+------+-----------+-------------+
2 rows in set (0.00 sec)