久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:148日期:2023-10-18 08:00:49
使用UNION操作符

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

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

演示

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

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

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

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

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

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

使用union 看一下效果:

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

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

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

使用union all 看一下效果:

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

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

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

拓展:

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

select t1.id id, t1.name name, t1.description description,t1.create_time time,’table1’ type from table1 t1UNION ALLselect t2.id id, t2.name name, t2.description description,t2.create_date time,’table2’ type from table2 t2

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

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

select t3.* from (select t1.id id, t1.name name, t1.description description,t1.create_time time,’table1’ type from table1 t1UNION ALLselect t2.id id, t2.name name, t2.description description,t2.create_date time,’table2’ type from table2 t2) t3 order by t3.time desc

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

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

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 崇阳县| 扎囊县| 黄梅县| 三亚市| 辽中县| 抚宁县| 古交市| 横山县| 中方县| 广德县| 宽城| 四川省| 广东省| 高雄市| 德安县| 玉门市| 平陆县| 福鼎市| 水富县| 新平| 赤城县| 延寿县| 奉新县| 南安市| 闸北区| 射阳县| 乳山市| 汕头市| 长岛县| 星子县| 视频| 岢岚县| 会理县| 临沧市| 舒城县| 长治市| 于田县| 孟津县| 科尔| 南投县| 全州县|