欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL數(shù)據(jù)庫(kù)子查詢?sub?query

 更新時(shí)間:2022年06月08日 08:39:17   作者:彭世瑜  
這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)子查詢?sub?query,子查詢指嵌套查詢下層的程序模塊,當(dāng)一個(gè)查詢是另一個(gè)查詢的條件的時(shí)候,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下下面文章內(nèi)容介紹

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)文章

  • 實(shí)例詳解mysql子查詢

    實(shí)例詳解mysql子查詢

    這篇文章主要介紹了mysql子查詢的相關(guān)資料,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫(kù),感興趣的朋友可以了解下
    2020-09-09
  • mysql select語(yǔ)句操作實(shí)例

    mysql select語(yǔ)句操作實(shí)例

    這篇文章主要介紹了mysql select語(yǔ)句操作實(shí)例,本文給出了ORDER BY查詢、GROUP BY查詢、LIMIT查詢、UNION等語(yǔ)句的實(shí)例,需要的朋友可以參考下
    2014-12-12
  • MySQL如何統(tǒng)計(jì)一個(gè)數(shù)據(jù)庫(kù)所有表的數(shù)據(jù)量

    MySQL如何統(tǒng)計(jì)一個(gè)數(shù)據(jù)庫(kù)所有表的數(shù)據(jù)量

    最近在做統(tǒng)計(jì)想查找一個(gè)數(shù)據(jù)庫(kù)里基本所有的表數(shù)據(jù)量,下面這篇文章主要給大家介紹了關(guān)于MySQL如何統(tǒng)計(jì)一個(gè)數(shù)據(jù)庫(kù)所有表的數(shù)據(jù)量的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • mysql數(shù)據(jù)庫(kù)連接失敗常見問題小結(jié)

    mysql數(shù)據(jù)庫(kù)連接失敗常見問題小結(jié)

    你有沒有碰到過(guò)mysql數(shù)據(jù)庫(kù)連接不上的問題呢?很多的小伙伴表示,經(jīng)常會(huì)時(shí)不時(shí)的出現(xiàn)這些問題,下面這篇文章主要給大家介紹了關(guān)于mysql數(shù)據(jù)庫(kù)連接失敗常見問題的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Linux下MySQL數(shù)據(jù)庫(kù)的主從同步復(fù)制配置

    Linux下MySQL數(shù)據(jù)庫(kù)的主從同步復(fù)制配置

    這篇文章主要介紹了Linux下MySQL數(shù)據(jù)庫(kù)的主從同步配置,
    2017-11-11
  • mysql時(shí)間字段默認(rèn)設(shè)置為當(dāng)前時(shí)間實(shí)例代碼

    mysql時(shí)間字段默認(rèn)設(shè)置為當(dāng)前時(shí)間實(shí)例代碼

    很多人可能會(huì)把日期類型的字段的類型設(shè)置為date或者datetime,
    但是這兩個(gè)類型是無(wú)法設(shè)置默認(rèn)值為當(dāng)前日期的,下面這篇文章主要給大家介紹了關(guān)于mysql時(shí)間字段默認(rèn)設(shè)置為當(dāng)前時(shí)間的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • mysql中insert語(yǔ)句的5種用法簡(jiǎn)單示例

    mysql中insert語(yǔ)句的5種用法簡(jiǎn)單示例

    這篇文章主要給大家介紹了關(guān)于mysql中insert語(yǔ)句的5種用法的相關(guān)資料,insert into是mysql中最常用的插入語(yǔ)句,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Windows安裝MySQL 5.7.18 解壓版的教程

    Windows安裝MySQL 5.7.18 解壓版的教程

    這篇文章主要為大家詳細(xì)介紹了Windows安裝MySQL 5.7.18 解壓版的詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • centos7.2離線安裝mysql5.7.18.tar.gz

    centos7.2離線安裝mysql5.7.18.tar.gz

    這篇文章主要為大家詳細(xì)介紹了centos7.2離線安裝mysql5.7.18.tar.gz,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • MySQL批量插入和唯一索引問題的解決方法

    MySQL批量插入和唯一索引問題的解決方法

    這篇文章主要給大家介紹了關(guān)于MySQL批量插入和唯一索引問題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論