溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL數(shù)據(jù)庫(kù)之怎么聯(lián)合查詢

發(fā)布時(shí)間:2022-06-01 13:46:50 來(lái)源:億速云 閱讀:189 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)之怎么聯(lián)合查詢的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇MySQL數(shù)據(jù)庫(kù)之怎么聯(lián)合查詢文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

前言:

將多個(gè)查詢結(jié)果的結(jié)果集合并到一起(縱向合并),字段數(shù)不變,多個(gè)查詢結(jié)果的記錄數(shù)合并

1、應(yīng)用場(chǎng)景

  • 同一張表中不同結(jié)果合并到一起展示:男生升高升序,女生升高降序

  • 數(shù)據(jù)量較大的表,進(jìn)行分表操作,將每張表的數(shù)據(jù)合并起來(lái)顯示

2、基本語(yǔ)法

select 語(yǔ)句
union [union 選項(xiàng)]
select 語(yǔ)句;

union 選項(xiàng) 和select 選項(xiàng)基本一致

  • distinct 去重,默認(rèn)

  • all 保存所有結(jié)果

mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  1 | 劉備   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |        2 |   20 |      2 |
|  7 | 張飛   |        2 |   21 |      1 |
|  8 | 關(guān)羽   |        1 |   22 |      2 |
|  9 | 曹操   |        1 |   20 |   NULL |
+----+--------+----------+------+--------+

-- 默認(rèn)選項(xiàng):distinct
select * from my_student
union
select * from my_student;
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  1 | 劉備   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |        2 |   20 |      2 |
|  7 | 張飛   |        2 |   21 |      1 |
|  8 | 關(guān)羽   |        1 |   22 |      2 |
|  9 | 曹操   |        1 |   20 |   NULL |
+----+--------+----------+------+--------+


select * from my_student
union all
select * from my_student;
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  1 | 劉備   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |        2 |   20 |      2 |
|  7 | 張飛   |        2 |   21 |      1 |
|  8 | 關(guān)羽   |        1 |   22 |      2 |
|  9 | 曹操   |        1 |   20 |   NULL |
|  1 | 劉備   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |        2 |   20 |      2 |
|  7 | 張飛   |        2 |   21 |      1 |
|  8 | 關(guān)羽   |        1 |   22 |      2 |
|  9 | 曹操   |        1 |   20 |   NULL |
+----+--------+----------+------+--------+

-- 只需要保證字段數(shù)量一樣,不需要每次拿到的數(shù)據(jù)類型都一樣
-- 只保留第一個(gè)select的字段名
select id, name, age from my_student
union all
select name, id, age  from my_student;
+--------+--------+------+
| id     | name   | age  |
+--------+--------+------+
| 1      | 劉備   |   18 |
| 2      | 李四   |   19 |
| 3      | 王五   |   20 |
| 7      | 張飛   |   21 |
| 8      | 關(guān)羽   |   22 |
| 9      | 曹操   |   20 |
| 劉備   | 1      |   18 |
| 李四   | 2      |   19 |
| 王五   | 3      |   20 |
| 張飛   | 7      |   21 |
| 關(guān)羽   | 8      |   22 |
| 曹操   | 9      |   20 |
+--------+--------+------+

3、order by的使用

聯(lián)合查詢中,使用order by, select語(yǔ)句必須使用括號(hào)

(select * from my_student where gender = 1 order by age desc)
union
(select * from my_student where gender = 2 order by age asc);
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  2 | 李四   |        1 |   19 |      1 |
|  7 | 張飛   |        2 |   21 |      1 |
|  1 | 劉備   |        1 |   18 |      2 |
|  3 | 王五   |        2 |   20 |      2 |
|  8 | 關(guān)羽   |        1 |   22 |      2 |
+----+--------+----------+------+--------+

-- order by 要生效,必須使用limit 通常大于表的記錄數(shù)
(select * from my_student where gender = 1 order by age desc limit 10)
union
(select * from my_student where gender = 2 order by age asc limit 10);
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  7 | 張飛   |        2 |   21 |      1 |
|  2 | 李四   |        1 |   19 |      1 |
|  1 | 劉備   |        1 |   18 |      2 |
|  3 | 王五   |        2 |   20 |      2 |
|  8 | 關(guān)羽   |        1 |   22 |      2 |
+----+--------+----------+------+--------+

關(guān)于“MySQL數(shù)據(jù)庫(kù)之怎么聯(lián)合查詢”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“MySQL數(shù)據(jù)庫(kù)之怎么聯(lián)合查詢”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI