最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

  • mysql 5.5.x zip直接解壓版安裝方法

    mysql 5.5.x zip直接解壓版安裝方法

    這篇文章主要介紹了mysql 5.5.x zip直接解壓版安裝方法 ,需要的朋友可以參考下
    2016-04-04
  • MySQL數(shù)據(jù)庫(kù)如何給表設(shè)置約束詳解

    MySQL數(shù)據(jù)庫(kù)如何給表設(shè)置約束詳解

    約束主要完成對(duì)數(shù)據(jù)的檢驗(yàn),保證數(shù)據(jù)庫(kù)數(shù)據(jù)的完整性;如果有相互依賴數(shù)據(jù),保證該數(shù)據(jù)不被刪除,本篇文章教你如何給表設(shè)置約束
    2022-03-03
  • MySQL進(jìn)階之索引

    MySQL進(jìn)階之索引

    索引就是一種數(shù)據(jù)結(jié)構(gòu),這種結(jié)構(gòu)類似,鏈表,樹等等。但是比它們要復(fù)雜的多,索引(index)是幫助MySQL高效獲取數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)(有序),本文詳細(xì)介紹了MySQL索引,感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • SQLyog錯(cuò)誤號(hào)碼2058最新解決辦法

    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
  • MySQL如何創(chuàng)建視圖

    MySQL如何創(chuàng)建視圖

    這篇文章主要介紹了MySQL如何創(chuàng)建視圖,幫助大家更好的理解和學(xué)習(xí)MySQL,感興趣的朋友可以了解下
    2020-08-08
  • MySQL SHOW STATUS語(yǔ)句的使用

    MySQL SHOW STATUS語(yǔ)句的使用

    這篇文章主要介紹了MySQL SHOW STATUS語(yǔ)句的使用,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫(kù),感興趣的朋友可以了解下
    2020-12-12
  • mysql數(shù)據(jù)庫(kù)是做什么的

    mysql數(shù)據(jù)庫(kù)是做什么的

    在本篇文章里小編給大家整理的是一篇關(guān)于mysql數(shù)據(jù)庫(kù)是做什么的先關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • my.cnf(my.ini)重要參數(shù)優(yōu)化配置說(shuō)明

    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
  • MySQL8的主要目錄結(jié)構(gòu)解讀

    MySQL8的主要目錄結(jié)構(gòu)解讀

    這篇文章主要介紹了MySQL8的主要目錄結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • MySQL ddl語(yǔ)句的使用

    MySQL ddl語(yǔ)句的使用

    這篇文章主要介紹了MySQL ddl語(yǔ)句的使用,幫助大家更好的理解和使用MySQL,感興趣的朋友可以了解下
    2020-11-11

最新評(píng)論

林口县| 互助| 阳新县| 获嘉县| 蓬安县| 阆中市| 徐闻县| 若羌县| 汉沽区| 札达县| 韶关市| 黑龙江省| 北票市| 黔西| 平南县| 罗定市| 阳山县| 凌云县| 庄河市| 安化县| 承德市| 榆林市| 界首市| 罗平县| 榆社县| 宜城市| 留坝县| 嘉定区| 西畴县| 阜新市| 保康县| 青川县| 军事| 那坡县| 安塞县| 赤峰市| 横峰县| 朝阳区| 东辽县| 海伦市| 浦江县|