亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MySQL數(shù)據(jù)庫(kù)之聯(lián)合查詢(xún)?union

 更新時(shí)間:2022年06月01日 11:02:29   作者:彭世瑜  
這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)之聯(lián)合查詢(xún)?union,聯(lián)合查詢(xún)就是將多個(gè)查詢(xún)結(jié)果的結(jié)果集合并到一起,字段數(shù)不變,多個(gè)查詢(xún)結(jié)果的記錄數(shù)合并,下文詳細(xì)介紹需要的小伙伴可以參考一下

前言:

將多個(gè)查詢(xún)結(jié)果的結(jié)果集合并到一起(縱向合并),字段數(shù)不變,多個(gè)查詢(xún)結(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ù)類(lèi)型都一樣
-- 只保留第一個(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)合查詢(xú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)合查詢(xún) union的文章就介紹到這了,更多相關(guān)MySQL聯(lián)合查詢(xún) union內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論