MySQL數(shù)據(jù)庫子查詢?sub?query
1、基本概念
1.1、子查詢
嵌套查詢下層的程序模塊,當(dāng)一個查詢是另一個查詢的條件時,稱之為子查詢
一條select語句中,嵌入了另一條select語句
1.2、主查詢
主要的查詢對象,第一條select語句,確定所獲取的數(shù)據(jù)目標(biāo)(數(shù)據(jù)源)
1.3、子查詢和主查詢的關(guān)系
- 子查詢是嵌入到主查詢中的
- 子查詢輔助主查詢,要么作為條件,要么作為數(shù)據(jù)源
- 子查詢可以獨立存在,是一條完整的select語句
1.4、子查詢的分類
1、按功能分
- 標(biāo)量子查詢:子查詢返回的結(jié)果是一個數(shù)據(jù)(一行一列)
- 列子查詢:返回一列(一列多行)
- 行子查詢:返回一行(一行多列)
- 表子查詢:返回多行多列
- exists子查詢 返回1或者0(類似布爾操作)
2、按位置分
- where子查詢
- from子查詢
2、標(biāo)量子查詢
查詢結(jié)果是一個數(shù)據(jù)(一行一列)
2.1、基本語法
-- 子查詢得到的結(jié)果只有一個值 select * from 數(shù)據(jù)源 where 條件判斷 =/<> (select 字段名 form 數(shù)據(jù)源 where 條件判斷);
2.2、示例
-- 知道學(xué)生的id,查詢所在班級名字 -- 主查詢:班級,子查詢:班級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、基本語法
主查詢 where 條件 in (列子查詢)
3.2、示例
-- 獲取有學(xué)生的班級名字 -- 1、找到學(xué)生表中的所有班級id -- 2、找出班級表中對應(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é)果是一行多列
- 字段元素:一個字段對應(yīng)的值
- 行元素:多個字段合起來作為一個元素參與運算
4.1、基本語法
主查詢 where 條件 [(構(gòu)造一個行元素)] = (行子查詢);
4.2、示例
獲取班級年齡最大,且班級號最大的學(xué)生
- 1、求年齡最大
- 2、求班級號最大
- 3、求出學(xué)生
-- 錯誤示例 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、基本語法
select 字段表 from (表子查詢) as 別名 [where] [group by] [having] [order by] [limit]
5.2、示例
獲取每個班級年齡最大的學(xué)生:
-- 錯誤示例 select * from my_student group by class_id having age = max(age); 將每個班年齡最大的學(xué)生排在最前面 order by 針對結(jié)果進行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、基本語法
where exists (查詢語句) -- 永遠為真 where 1;
6.2、示例
-- 查詢有學(xué)生的所有班級 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é)果中有任意一個匹配即可,等價于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 | +----+--------+----------+------+--------+ -- 不等于任意一個 主查詢 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ù)庫子查詢 sub query的文章就介紹到這了,更多相關(guān)MySQL sub query內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL如何統(tǒng)計一個數(shù)據(jù)庫所有表的數(shù)據(jù)量
最近在做統(tǒng)計想查找一個數(shù)據(jù)庫里基本所有的表數(shù)據(jù)量,下面這篇文章主要給大家介紹了關(guān)于MySQL如何統(tǒng)計一個數(shù)據(jù)庫所有表的數(shù)據(jù)量的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04mysql數(shù)據(jù)庫連接失敗常見問題小結(jié)
你有沒有碰到過mysql數(shù)據(jù)庫連接不上的問題呢?很多的小伙伴表示,經(jīng)常會時不時的出現(xiàn)這些問題,下面這篇文章主要給大家介紹了關(guān)于mysql數(shù)據(jù)庫連接失敗常見問題的相關(guān)資料,需要的朋友可以參考下2023-06-06Linux下MySQL數(shù)據(jù)庫的主從同步復(fù)制配置
這篇文章主要介紹了Linux下MySQL數(shù)據(jù)庫的主從同步配置,2017-11-11mysql時間字段默認設(shè)置為當(dāng)前時間實例代碼
很多人可能會把日期類型的字段的類型設(shè)置為date或者datetime,2022-08-08
但是這兩個類型是無法設(shè)置默認值為當(dāng)前日期的,下面這篇文章主要給大家介紹了關(guān)于mysql時間字段默認設(shè)置為當(dāng)前時間的相關(guān)資料,需要的朋友可以參考下centos7.2離線安裝mysql5.7.18.tar.gz
這篇文章主要為大家詳細介紹了centos7.2離線安裝mysql5.7.18.tar.gz,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06