MySQL數(shù)據(jù)庫(kù)之聯(lián)合查詢?union
前言:
將多個(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ù)類(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)合查詢中,使用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)合查詢 union的文章就介紹到這了,更多相關(guān)MySQL聯(lián)合查詢 union內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL字符串索引更合理的創(chuàng)建規(guī)則討論
這篇文章主要給大家介紹了關(guān)于MySQL字符串索引更合理的創(chuàng)建規(guī)則,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
mysql中json類(lèi)型字段的基本用法實(shí)例
大家應(yīng)該都知道m(xù)ysql中的字段可以使用json的格式來(lái)進(jìn)行存儲(chǔ),下面這篇文章主要給大家介紹了關(guān)于mysql中json類(lèi)型字段的基本用法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
重置MySQL數(shù)據(jù)庫(kù)root密碼(linux/windows)
linux與windows下重置mysql用戶名與密碼的方法,需要的朋友可以參考下。2010-12-12
MySQL中Nested-Loop Join算法小結(jié)
數(shù)據(jù)庫(kù)中JOIN操作的實(shí)現(xiàn)主要有三種:嵌套循環(huán)連接(Nested Loop Join),歸并連接(Merge Join)和散列連接或者哈稀連接(Hash Join)。其中嵌套循環(huán)連接又視情況又有兩種變形:塊嵌套循環(huán)連接和索引嵌套循環(huán)連接。2015-12-12
阿里云ECS centos6.8下安裝配置MySql5.7的教程
阿里云默認(rèn)yum命令下的MySQL是5.17****,安裝mysql5.7之前先卸載以前的版本。下面通過(guò)本文給大家介紹阿里云ECS centos6.8下安裝配置MySql5.7的教程,需要的的朋友參考下吧2017-07-07
MySQL Community Server 8.0.11安裝配置方法圖文教程
這篇文章主要為大家詳細(xì) 介紹了MySQL Community Server 8.0.11安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
mysql基礎(chǔ):mysqld_safe 啟動(dòng)執(zhí)行流程詳解
本篇文章是對(duì)mysql基礎(chǔ)中的mysqld_safe啟動(dòng)執(zhí)行流程進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

