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

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

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

1、基本概念

1.1、子查詢

嵌套查詢下層的程序模塊,當一個查詢是另一個查詢的條件時,稱之為子查詢

一條select語句中,嵌入了另一條select語句

1.2、主查詢

主要的查詢對象,第一條select語句,確定所獲取的數(shù)據(jù)目標(數(shù)據(jù)源)

1.3、子查詢和主查詢的關系

  • 子查詢是嵌入到主查詢中的
  • 子查詢輔助主查詢,要么作為條件,要么作為數(shù)據(jù)源
  • 子查詢可以獨立存在,是一條完整的select語句

1.4、子查詢的分類

1、按功能分

  • 標量子查詢:子查詢返回的結果是一個數(shù)據(jù)(一行一列)
  • 列子查詢:返回一列(一列多行)
  • 行子查詢:返回一行(一行多列)
  • 表子查詢:返回多行多列
  • exists子查詢 返回1或者0(類似布爾操作)

2、按位置分

  • where子查詢
  • from子查詢

2、標量子查詢

查詢結果是一個數(shù)據(jù)(一行一列)

2.1、基本語法

-- 子查詢得到的結果只有一個值
select * from 數(shù)據(jù)源 where 條件判斷 =/<> (select 字段名 form 數(shù)據(jù)源 where 條件判斷);

2.2、示例

-- 知道學生的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 | 關羽   |        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、列子查詢

列子查詢得到的結果是一列數(shù)據(jù),一列多行

3.1、基本語法

主查詢 where 條件 in (列子查詢)

3.2、示例

-- 獲取有學生的班級名字
-- 1、找到學生表中的所有班級id
-- 2、找出班級表中對應的名字

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、行子查詢

行子查詢返回的結果是一行多列

  • 字段元素:一個字段對應的值
  • 行元素:多個字段合起來作為一個元素參與運算

4.1、基本語法

主查詢 where 條件 [(構造一個行元素)] = (行子查詢);

4.2、示例

獲取班級年齡最大,且班級號最大的學生

  • 1、求年齡最大
  • 2、求班級號最大
  • 3、求出學生
-- 錯誤示例
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í)行,結果就是只返回一行記錄,第一行 

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 | 關羽   |        1 |   22 |      2 |
+----+--------+----------+------+--------+

總結:

標量子查詢、列子查詢、行子查詢都屬于where子查詢

5、表子查詢

表子查詢返回結果是多行多列,與行子查詢相似

行子查詢需要行元素,表子查詢沒有 

  • 行子查詢用于where條件判斷,屬于where子查詢
  • 表子查詢用于from數(shù)據(jù)源,屬于from子查詢

5.1、基本語法

select 字段表 from (表子查詢) as 別名 [where] [group by] [having] [order by] [limit]

5.2、示例

獲取每個班級年齡最大的學生:

-- 錯誤示例
select * from my_student group by class_id having age = max(age);
將每個班年齡最大的學生排在最前面 order by
針對結果進行group 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子查詢

返回結果只有0或者1,1代表成立,0代表不成立

6.1、基本語法

where exists (查詢語句)
-- 永遠為真
where 1;

6.2、示例

-- 查詢有學生的所有班級
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、子查詢中的特定關鍵字

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 | 關羽   |        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 | 關羽   |        1 |   22 |      2 |
|  6 | 曹操   |        1 |   20 |   NULL |
+----+--------+----------+------+--------+

7.2、any

-- 查詢結果中有任意一個匹配即可,等價于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 | 關羽   |        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 | 關羽   |        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 | 關羽   |     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 |
+----+--------+----------+------+--------+

到此這篇關于MySQL數(shù)據(jù)庫子查詢 sub query的文章就介紹到這了,更多相關MySQL sub query內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

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

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

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

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

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

    MySQL進階之索引

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

    SQLyog錯誤號碼2058最新解決辦法

    這篇文章主要給大家介紹了關于SQLyog錯誤號碼2058的最新解決辦法,使用sqlyog連接數(shù)據(jù)庫過程中可能會出現(xiàn)2058錯誤,出現(xiàn)的原因是因為MYSQL8.0對密碼的加密方式進行了改變,需要的朋友可以參考下
    2023-08-08
  • MySQL如何創(chuàng)建視圖

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

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

    MySQL SHOW STATUS語句的使用

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

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

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

    my.cnf(my.ini)重要參數(shù)優(yōu)化配置說明

    本文針對mysql不同存儲引擎,MyISAM與Innodb進行了講解如何進行my.cnf(my.ini)的參數(shù)優(yōu)化
    2018-03-03
  • MySQL8的主要目錄結構解讀

    MySQL8的主要目錄結構解讀

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

    MySQL ddl語句的使用

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

最新評論

江华| 平武县| 颍上县| 天峨县| 巴马| 翼城县| 永吉县| 米林县| 阳曲县| 高密市| 开平市| 嘉定区| 浏阳市| 柳江县| 威远县| 博乐市| 永靖县| 璧山县| 东丰县| 巧家县| 曲水县| 桂阳县| 东丽区| 绿春县| 老河口市| 扎赉特旗| 茂名市| 墨玉县| 贞丰县| 灵璧县| 彩票| 色达县| 虹口区| 团风县| 军事| 沙河市| 凤翔县| 建始县| 大邑县| 新竹县| 河南省|