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

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

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

前言:

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

1、應用場景

  • 同一張表中不同結果合并到一起展示:男生升高升序,女生升高降序
  • 數(shù)據(jù)量較大的表,進行分表操作,將每張表的數(shù)據(jù)合并起來顯示

2、基本語法

select 語句
union [union 選項]
select 語句;

union 選項 和select 選項基本一致

  • distinct 去重,默認
  • all 保存所有結果
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 | 關羽   |        1 |   22 |      2 |
|  9 | 曹操   |        1 |   20 |   NULL |
+----+--------+----------+------+--------+

-- 默認選項: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 | 關羽   |        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 | 關羽   |        1 |   22 |      2 |
|  9 | 曹操   |        1 |   20 |   NULL |
|  1 | 劉備   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |        2 |   20 |      2 |
|  7 | 張飛   |        2 |   21 |      1 |
|  8 | 關羽   |        1 |   22 |      2 |
|  9 | 曹操   |        1 |   20 |   NULL |
+----+--------+----------+------+--------+

-- 只需要保證字段數(shù)量一樣,不需要每次拿到的數(shù)據(jù)類型都一樣
-- 只保留第一個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      | 關羽   |   22 |
| 9      | 曹操   |   20 |
| 劉備   | 1      |   18 |
| 李四   | 2      |   19 |
| 王五   | 3      |   20 |
| 張飛   | 7      |   21 |
| 關羽   | 8      |   22 |
| 曹操   | 9      |   20 |
+--------+--------+------+

3、order by的使用

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

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

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

相關文章

最新評論