MySQL數(shù)據(jù)庫之聯(lián)合查詢?union
前言:
將多個查詢結(jié)果的結(jié)果集合并到一起(縱向合并),字段數(shù)不變,多個查詢結(jié)果的記錄數(shù)合并
1、應(yīng)用場景
- 同一張表中不同結(jié)果合并到一起展示:男生升高升序,女生升高降序
- 數(shù)據(jù)量較大的表,進(jìn)行分表操作,將每張表的數(shù)據(jù)合并起來顯示
2、基本語法
select 語句 union [union 選項] select 語句;
union 選項 和select 選項基本一致
- 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)選項: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ù)類型都一樣 -- 只保留第一個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語句必須使用括號
(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ù)庫之聯(lián)合查詢 union的文章就介紹到這了,更多相關(guān)MySQL聯(lián)合查詢 union內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL中的alter table命令的基本使用方法及提速優(yōu)化
這篇文章主要介紹了MySQL中的alter table命令的基本使用方法及提速優(yōu)化的方法,包括ALTER COLUMN的使用等等,需要的朋友可以參考下2015-11-11mysql使用GROUP BY分組實現(xiàn)取前N條記錄的方法
這篇文章主要介紹了mysql使用GROUP BY分組實現(xiàn)取前N條記錄的方法,結(jié)合實例形式較為詳細(xì)的分析了mysql中GROUP BY分組的相關(guān)使用技巧,需要的朋友可以參考下2016-06-06淺析一個MYSQL語法(在查詢中使用count)的兼容性問題
本篇文章是對MYSQL語法(在查詢中使用count)的兼容性問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07淺談MySQL 億級數(shù)據(jù)分頁的優(yōu)化
mysql大數(shù)據(jù)量使用limit分頁,隨著頁碼的增大,查詢效率越低下。本文就來介紹一下MySQL 億級數(shù)據(jù)分頁的優(yōu)化,感興趣的小伙伴們可以參考一下2021-06-06mysql5.7.18安裝時mysql服務(wù)啟動失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了mysql5.7.18安裝時mysql服務(wù)啟動失敗的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03