1. 程式人生 > 資料庫 >MySQL資料庫之子查詢

MySQL資料庫之子查詢

子查詢

  • 語法

    • select * from 表1 where (子查詢)
  • 說明

    • 外面的查詢稱為父查詢
    • 子查詢為父查詢提供查詢條件

標量子查詢

  • 特點
    • 子查詢返回的值是一個
MariaDB [sel]> select * from resume where name=(select name from resume where id=3);
+----+-------+-----------+
| id | name  | skill     |
+----+-------+-----------+
|  3 | Jerry | php,mysql |
+----+-------+-----------+
# `1 row in set (0.010 sec)`
  • 找到數學最高分
MariaDB [sel]> select * from grades where name=(select name from grades order by math desc limit 1);
+-------+---------+------+
| name  | chinese | math |
+-------+---------+------+
| Sunny |      93 |   96 |
+-------+---------+------+
# `1 row in set (0.004 sec)`
MariaDB [sel]>  select * from grades where name=(select name from grades where math=(select max(math) from grades));
+-------+---------+------+
| name  | chinese | math |
+-------+---------+------+
| Sunny |      93 |   96 |
+-------+---------+------+
# `1 row in set (0.001 sec)`

列子查詢

  • 特點
    • 子查詢返回的結果是一列
    • 如果子查詢的結果返回多條記錄,不能使用等於,用in或not in
MariaDB [sel]> select * from grades where name in (select name from grades where math>92);
+-------+---------+------+
| name  | chinese | math |
+-------+---------+------+
| Sunny |      93 |   96 |
| Marry |      95 |   94 |
| Tommy |      98 |   94 |
+-------+---------+------+
# `3 rows in set (0.007 sec)`
MariaDB [sel]> select * from grades where name not in (select name from grades where math>92);
+-------+---------+------+
| name  | chinese | math |
+-------+---------+------+
| Jerry |      97 |   91 |
+-------+---------+------+
# `1 row in set (0.008 sec)`

MariaDB [sel]> select * from grades where name in (select name from grades where math<92);
+-------+---------+------+
| name  | chinese | math |
+-------+---------+------+
| Jerry |      97 |   91 |
+-------+---------+------+
# `1 row in set (0.000 sec)`

行子查詢

  • 特點
    • 子查詢返回的結果是多個欄位組成
MariaDB [sel]> select * from grades where (sex,math) in (select sex,max(math) from grades group by sex);
+-------+------+---------+------+
| name  | sex  | chinese | math |
+-------+------+---------+------+
| Sunny | boy  |      93 |   96 |
| Marry | girl |      95 |   94 |
+-------+------+---------+------+
# `2 rows in set (0.001 sec)`

表子查詢

  • 特點
    • 將子查詢的結果作為表
    • from後面跟的是資料來源,如果將子查詢當成表來看, 必須給結果集取別名
MariaDB [sel]> select * from (select * from grades order by math desc) best group by sex;
+-------+------+---------+------+
| name  | sex  | chinese | math |
+-------+------+---------+------+
| Sunny | boy  |      93 |   96 |
| Marry | girl |      95 |   94 |
+-------+------+---------+------+
# `2 rows in set (0.007 sec)`

存在子查詢

  • 特點
    • 提高查詢效率
MariaDB [sel]> select * from grades where exists (select * from grades where math>90);
+-------+------+---------+------+
| name  | sex  | chinese | math |
+-------+------+---------+------+
| Sunny | boy  |      93 |   96 |
| Jerry | boy  |      97 |   91 |
| Marry | girl |      95 |   94 |
| Tommy | boy  |      98 |   94 |
+-------+------+---------+------+
# `4 rows in set (0.001 sec)`
MariaDB [sel]> select * from grades where not exists (select * from grades where math>90);
# `Empty set (0.001 sec)`