濮阳杆衣贸易有限公司

主頁 > 知識庫 > MYSQL使用Union將兩張表的數(shù)據(jù)合并顯示

MYSQL使用Union將兩張表的數(shù)據(jù)合并顯示

熱門標簽:拉卡拉外呼系統(tǒng) 智能外呼系統(tǒng)復位 話務(wù)外呼系統(tǒng)怎么樣 高清地圖標注道路 云南電商智能外呼系統(tǒng)價格 臨清電話機器人 外東北地圖標注 大眾點評星級酒店地圖標注 400電話可以辦理嗎

使用UNION操作符

union:用于連接兩個以上的 SELECT 語句的結(jié)果組合到一個結(jié)果集合中。多個 SELECT 語句會刪除重復的數(shù)據(jù)。

使用union操作符會將多張表中相同的數(shù)據(jù)取值一次,如果想將表1和表2中的值完整的顯示出來,可以使用union all。

演示

小伙伴們自行創(chuàng)建一下表。

表1數(shù)據(jù)如下: 

表2數(shù)據(jù)如下:

OK,表數(shù)據(jù)已經(jīng)創(chuàng)建完成,一共五條數(shù)據(jù),接下來我們?nèi)タ匆豢磚nion 和 union all 的使用。

使用union 看一下效果:

select t1.id id, t1.name name, t1.description description,t1.create_time time from table1 t1
UNION
select t2.id id, t2.name name, t2.description description,t2.create_date time from table2 t2

我們可以看到使用union只會查出來四條數(shù)據(jù)。其中兩條是相同的數(shù)據(jù),則顯示一條。

使用union all 看一下效果:

select t1.id id, t1.name name, t1.description description,t1.create_time time from table1 t1
UNION ALL
select t2.id id, t2.name name, t2.description description,t2.create_date time from table2 t2

使用union all查出5條數(shù)據(jù),ps:相同的數(shù)據(jù)也會查詢出來。

拓展: 

為了區(qū)分哪張表中的數(shù)據(jù),我們可以這樣做

select t1.id id, t1.name name, t1.description description,t1.create_time time,'table1' type from table1 t1
UNION ALL
select t2.id id, t2.name name, t2.description description,t2.create_date time,'table2' type  from table2 t2

將兩張表中的數(shù)據(jù)按時間排序

select t3.* from (select t1.id id, t1.name name, t1.description description,t1.create_time time,'table1' type from table1 t1
UNION ALL
select t2.id id, t2.name name, t2.description description,t2.create_date time,'table2' type  from table2 t2) t3 order by t3.time desc

到此這篇關(guān)于MYSQL使用Union將兩張表的數(shù)據(jù)合并顯示的文章就介紹到這了,更多相關(guān)mysql數(shù)據(jù)合并顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MySQL union 語法代碼示例分析
  • mySQL UNION運算符的默認規(guī)則研究
  • MySQL如何使用union all獲得并集排序
  • MySQL中UNION與UNION ALL的基本使用方法
  • 淺析mysql union和union all
  • mysql如何將多行數(shù)據(jù)合并成一行

標簽:阿里 無錫 福州 定西 山西 揚州 溫州 三明

巨人網(wǎng)絡(luò)通訊聲明:本文標題《MYSQL使用Union將兩張表的數(shù)據(jù)合并顯示》,本文關(guān)鍵詞  MYSQL,使用,Union,將,兩張,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MYSQL使用Union將兩張表的數(shù)據(jù)合并顯示》相關(guān)的同類信息!
  • 本頁收集關(guān)于MYSQL使用Union將兩張表的數(shù)據(jù)合并顯示的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    永川市| 澄迈县| 收藏| 宜章县| 阿克陶县| 黎川县| 古丈县| 安宁市| 巢湖市| 清水河县| 南宁市| 从化市| 勃利县| 汉寿县| 临海市| 司法| 宿迁市| 吴堡县| 河北省| 保靖县| 泸定县| 法库县| 永安市| 九江市| 桐梓县| 和政县| 大荔县| 依兰县| 武川县| 积石山| 台东县| 河东区| 田林县| 贵溪市| 贺兰县| 柏乡县| 泸溪县| 永靖县| 北安市| 绵竹市| 揭西县|