MySQL數(shù)據(jù)庫(kù)子查詢?sub?query
1、基本概念
1.1、子查詢
嵌套查詢下層的程序模塊,當(dāng)一個(gè)查詢是另一個(gè)查詢的條件時(shí),稱之為子查詢
一條select語(yǔ)句中,嵌入了另一條select語(yǔ)句
1.2、主查詢
主要的查詢對(duì)象,第一條select語(yǔ)句,確定所獲取的數(shù)據(jù)目標(biāo)(數(shù)據(jù)源)
1.3、子查詢和主查詢的關(guān)系
- 子查詢是嵌入到主查詢中的
- 子查詢輔助主查詢,要么作為條件,要么作為數(shù)據(jù)源
- 子查詢可以獨(dú)立存在,是一條完整的select語(yǔ)句
1.4、子查詢的分類
1、按功能分
- 標(biāo)量子查詢:子查詢返回的結(jié)果是一個(gè)數(shù)據(jù)(一行一列)
- 列子查詢:返回一列(一列多行)
- 行子查詢:返回一行(一行多列)
- 表子查詢:返回多行多列
- exists子查詢 返回1或者0(類似布爾操作)
2、按位置分
- where子查詢
- from子查詢
2、標(biāo)量子查詢
查詢結(jié)果是一個(gè)數(shù)據(jù)(一行一列)
2.1、基本語(yǔ)法
-- 子查詢得到的結(jié)果只有一個(gè)值 select * from 數(shù)據(jù)源 where 條件判斷 =/<> (select 字段名 form 數(shù)據(jù)源 where 條件判斷);
2.2、示例
-- 知道學(xué)生的id,查詢所在班級(jí)名字
-- 主查詢:班級(jí),子查詢:班級(jí)id
mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 1 | 劉備 | 1 | 18 | 2 |
| 2 | 李四 | 1 | 19 | 1 |
| 3 | 王五 | 2 | 20 | 2 |
| 4 | 張飛 | 2 | 21 | 1 |
| 5 | 關(guān)羽 | 1 | 22 | 2 |
| 6 | 曹操 | 1 | 20 | NULL |
+----+--------+----------+------+--------+
mysql> select * from my_class;
+----+--------+
| id | name |
+----+--------+
| 1 | 一班 |
| 3 | 三班 |
| 2 | 二班 |
+----+--------+
select class_id from my_student where id = 1;
+----------+
| class_id |
+----------+
| 1 |
+----------+
select * from my_class where id = (
select class_id from my_student where id = 1
);
+----+--------+
| id | name |
+----+--------+
| 1 | 一班 |
+----+--------+
3、列子查詢
列子查詢得到的結(jié)果是一列數(shù)據(jù),一列多行
3.1、基本語(yǔ)法
主查詢 where 條件 in (列子查詢)
3.2、示例
-- 獲取有學(xué)生的班級(jí)名字
-- 1、找到學(xué)生表中的所有班級(jí)id
-- 2、找出班級(jí)表中對(duì)應(yīng)的名字
select distinct(class_id) from my_student;
+----------+
| class_id |
+----------+
| 1 |
| 2 |
+----------+
select name from my_class where id in (
select distinct(class_id) from my_student
);
+--------+
| name |
+--------+
| 一班 |
| 二班 |
+--------+
4、行子查詢
行子查詢返回的結(jié)果是一行多列
- 字段元素:一個(gè)字段對(duì)應(yīng)的值
- 行元素:多個(gè)字段合起來(lái)作為一個(gè)元素參與運(yùn)算
4.1、基本語(yǔ)法
主查詢 where 條件 [(構(gòu)造一個(gè)行元素)] = (行子查詢);
4.2、示例
獲取班級(jí)年齡最大,且班級(jí)號(hào)最大的學(xué)生
- 1、求年齡最大
- 2、求班級(jí)號(hào)最大
- 3、求出學(xué)生
-- 錯(cuò)誤示例
select * from my_student having age = max(age) and class_id = max(class_id);
-- 1、having在group by之后,代表group by執(zhí)行了一次,聚合函數(shù)使用
-- 2、group by 一旦執(zhí)行,結(jié)果就是只返回一行記錄,第一行
select max(age), max(class_id) from my_student;
+----------+---------------+
| max(age) | max(class_id) |
+----------+---------------+
| 22 | 2 |
+----------+---------------+
select * from my_student where (age, class_id) = (
select max(age), max(class_id) from my_student
);
Empty set (0.01 sec)
select * from my_student where (age, class_id) = (
select max(age), min(class_id) from my_student
);
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 5 | 關(guān)羽 | 1 | 22 | 2 |
+----+--------+----------+------+--------+總結(jié):
標(biāo)量子查詢、列子查詢、行子查詢都屬于where子查詢
5、表子查詢
表子查詢返回結(jié)果是多行多列,與行子查詢相似
行子查詢需要行元素,表子查詢沒有
- 行子查詢用于where條件判斷,屬于where子查詢
- 表子查詢用于from數(shù)據(jù)源,屬于from子查詢
5.1、基本語(yǔ)法
select 字段表 from (表子查詢) as 別名 [where] [group by] [having] [order by] [limit]
5.2、示例
獲取每個(gè)班級(jí)年齡最大的學(xué)生:
-- 錯(cuò)誤示例
select * from my_student group by class_id having age = max(age);
將每個(gè)班年齡最大的學(xué)生排在最前面 order by
針對(duì)結(jié)果進(jìn)行g(shù)roup by 保留每組第一條數(shù)據(jù)
--
select * from (
select * from my_student order by age desc
) as t group by t.class_id;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 1 | 劉備 | 1 | 18 | 2 |
| 3 | 王五 | 2 | 20 | 2 |
+----+--------+----------+------+--------+
6、exists子查詢
返回結(jié)果只有0或者1,1代表成立,0代表不成立
6.1、基本語(yǔ)法
where exists (查詢語(yǔ)句) -- 永遠(yuǎn)為真 where 1;
6.2、示例
-- 查詢有學(xué)生的所有班級(jí)
select * from my_class as c where exists (
select id from my_student as s where s.class_id = c.id
);
+----+--------+
| id | name |
+----+--------+
| 1 | 一班 |
| 2 | 二班 |
+----+--------+7、子查詢中的特定關(guān)鍵字
mysql> select * from my_student; +----+--------+----------+------+--------+ | id | name | class_id | age | gender | +----+--------+----------+------+--------+ | 1 | 劉備 | 1 | 18 | 2 | | 2 | 李四 | 1 | 19 | 1 | | 3 | 王五 | 2 | 20 | 2 | | 4 | 張飛 | 2 | 21 | 1 | | 5 | 關(guān)羽 | 1 | 22 | 2 | | 6 | 曹操 | 1 | 20 | NULL | +----+--------+----------+------+--------+ mysql> select id from my_class; +----+ | id | +----+ | 1 | | 3 | | 2 | +----+
7.1、in
主查詢 where 條件 in (列子查詢)
select * from my_student where class_id in (select id from my_class); +----+--------+----------+------+--------+ | id | name | class_id | age | gender | +----+--------+----------+------+--------+ | 1 | 劉備 | 1 | 18 | 2 | | 2 | 李四 | 1 | 19 | 1 | | 3 | 王五 | 2 | 20 | 2 | | 4 | 張飛 | 2 | 21 | 1 | | 5 | 關(guān)羽 | 1 | 22 | 2 | | 6 | 曹操 | 1 | 20 | NULL | +----+--------+----------+------+--------+
7.2、any
-- 查詢結(jié)果中有任意一個(gè)匹配即可,等價(jià)于in 主查詢 where 條件 any (列子查詢) select * from my_student where class_id = any (select id from my_class); +----+--------+----------+------+--------+ | id | name | class_id | age | gender | +----+--------+----------+------+--------+ | 1 | 劉備 | 1 | 18 | 2 | | 2 | 李四 | 1 | 19 | 1 | | 3 | 王五 | 2 | 20 | 2 | | 4 | 張飛 | 2 | 21 | 1 | | 5 | 關(guān)羽 | 1 | 22 | 2 | | 6 | 曹操 | 1 | 20 | NULL | +----+--------+----------+------+--------+ -- 不等于任意一個(gè) 主查詢 where 條件 any <> (列子查詢) select * from my_student where class_id <> any (select id from my_class); +----+--------+----------+------+--------+ | id | name | class_id | age | gender | +----+--------+----------+------+--------+ | 1 | 劉備 | 1 | 18 | 2 | | 2 | 李四 | 1 | 19 | 1 | | 3 | 王五 | 2 | 20 | 2 | | 4 | 張飛 | 2 | 21 | 1 | | 5 | 關(guān)羽 | 1 | 22 | 2 | | 6 | 曹操 | 1 | 20 | NULL | +----+--------+----------+------+--------+
7.3、some
與any完全一樣
7.4、all
-- 等于其中所有 =all(列子查詢) select * from my_student where class_id = all (select id from my_class); Empty set (0.00 sec) select * from my_class where id = all (select class_id from my_student); Empty set (0.00 sec) -- 不等于其中所有 <>all(列子查詢) select * from my_student where class_id <> all (select id from my_class); Empty set (0.00 sec) select * from my_class where id <> all (select class_id from my_student); +----+--------+ | id | name | +----+--------+ | 3 | 三班 | +----+--------+
7.5、值為null
如果值為null,不參與匹配
mysql> select * from my_student; +----+--------+----------+------+--------+ | id | name | class_id | age | gender | +----+--------+----------+------+--------+ | 1 | 劉備 | 1 | 18 | 2 | | 2 | 李四 | 1 | 19 | 1 | | 3 | 王五 | 2 | 20 | 2 | | 4 | 張飛 | 2 | 21 | 1 | | 5 | 關(guān)羽 | NULL | 22 | 2 | | 6 | 曹操 | 1 | 20 | NULL | +----+--------+----------+------+--------+ mysql> select * from my_student where class_id = any (select id from my_class); +----+--------+----------+------+--------+ | id | name | class_id | age | gender | +----+--------+----------+------+--------+ | 1 | 劉備 | 1 | 18 | 2 | | 2 | 李四 | 1 | 19 | 1 | | 3 | 王五 | 2 | 20 | 2 | | 4 | 張飛 | 2 | 21 | 1 | | 6 | 曹操 | 1 | 20 | NULL | +----+--------+----------+------+--------+ mysql> select * from my_student where class_id <> any (select id from my_class); +----+--------+----------+------+--------+ | id | name | class_id | age | gender | +----+--------+----------+------+--------+ | 1 | 劉備 | 1 | 18 | 2 | | 2 | 李四 | 1 | 19 | 1 | | 3 | 王五 | 2 | 20 | 2 | | 4 | 張飛 | 2 | 21 | 1 | | 6 | 曹操 | 1 | 20 | NULL | +----+--------+----------+------+--------+
到此這篇關(guān)于MySQL數(shù)據(jù)庫(kù)子查詢 sub query的文章就介紹到這了,更多相關(guān)MySQL sub query內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)庫(kù)如何給表設(shè)置約束詳解
約束主要完成對(duì)數(shù)據(jù)的檢驗(yàn),保證數(shù)據(jù)庫(kù)數(shù)據(jù)的完整性;如果有相互依賴數(shù)據(jù),保證該數(shù)據(jù)不被刪除,本篇文章教你如何給表設(shè)置約束2022-03-03
SQLyog錯(cuò)誤號(hào)碼2058最新解決辦法
這篇文章主要給大家介紹了關(guān)于SQLyog錯(cuò)誤號(hào)碼2058的最新解決辦法,使用sqlyog連接數(shù)據(jù)庫(kù)過程中可能會(huì)出現(xiàn)2058錯(cuò)誤,出現(xiàn)的原因是因?yàn)镸YSQL8.0對(duì)密碼的加密方式進(jìn)行了改變,需要的朋友可以參考下2023-08-08
my.cnf(my.ini)重要參數(shù)優(yōu)化配置說(shuō)明
本文針對(duì)mysql不同存儲(chǔ)引擎,MyISAM與Innodb進(jìn)行了講解如何進(jìn)行my.cnf(my.ini)的參數(shù)優(yōu)化2018-03-03

