首頁 > 軟體

MySQL兩個查詢如何合併成一個結果詳解

2022-09-02 18:03:53

MySQL 查詢合併

如果我們需要將兩個select語句的結果作為一個整體顯示出來,我們就需要用到union或者union all關鍵字。union(或稱為聯合)的作用是將多個結果合併在一起顯示出來。

注意:兩個列表中的欄位要一樣才可以合併(順序也要一樣)

滿足條件:

1、兩個select查詢的列的數量必須相同;

2、每個列的資料型別需要相似;

1.先寫兩條select

第一條select:

SELECT
	DATE_FORMAT(add_time, '%Y-%m-%d') as 'add_time',
	COUNT(add_time) as 'reach_intention'
FROM
	table1
where
## where 條件可以根據自己實際情況來定
	DATE_FORMAT(add_time, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 1 month), '%Y-%m-%d')
group by
	DATE_FORMAT(add_time, '%Y-%m-%d')

這是查詢出來的結果

第二條select:

select
	DATE_FORMAT(add_date, '%Y-%m-%d') as 'add_time' ,
	COUNT(add_date)  as 'post_release'
from
	table2
where
## where 條件可以根據自己實際情況來定
	DATE_FORMAT(add_date, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 1 month), '%Y-%m-%d')
group by
	DATE_FORMAT(add_date, '%Y-%m-%d')

第二條select查詢出來的結果

2.合併查詢結果

先把兩條select用union all連線起來

SELECT
		DATE_FORMAT(add_time, '%Y-%m-%d') as 'add_time', COUNT(add_time) as 'reach_intention' ,  '' as 'post_release'
	FROM
		table1
	where
		DATE_FORMAT(add_time, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 30 day), '%Y-%m-%d')
	group by
		DATE_FORMAT(add_time, '%Y-%m-%d')
UNION ALL
	select
		DATE_FORMAT(add_date, '%Y-%m-%d') as 'add_time' , '' as 'reach_intention' ,  COUNT(add_date) as 'post_release'
	from
		table2
	where
		DATE_FORMAT(add_date, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 30 day), '%Y-%m-%d')
	group by
		DATE_FORMAT(add_date, '%Y-%m-%d') 

在外層巢狀一個select

## 括號裡面放 用 union all 連線的select 
select * from () test
## 這裡要給表起個別名 不然會報 Every derived table must have its own alias 每個派生表都必須有自己的別名

把union all 連線的select 放到括號裡面去然後執行

select
	*
from
(
	SELECT
		DATE_FORMAT(add_time, '%Y-%m-%d') as 'add_time', COUNT(add_time) as 'reach_intention' ,  '' as 'post_release'
	FROM
		table1
	where
		DATE_FORMAT(add_time, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 30 day), '%Y-%m-%d')
	group by
		DATE_FORMAT(add_time, '%Y-%m-%d')
UNION ALL
	select
		DATE_FORMAT(add_date, '%Y-%m-%d') as 'add_time' , '' as 'reach_intention' ,  COUNT(add_date) as 'post_release'
	from
		table2
	where
		DATE_FORMAT(add_date, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 30 day), '%Y-%m-%d')
	group by
		DATE_FORMAT(add_date, '%Y-%m-%d') 
) test

執行結果

把結果按日期分組排序。

使用 group by 和 order by 關鍵字

select
	add_time,
	sum(reach_intention) as 'reach_intention' ,
	sum(post_release) as 'post_release'
from
	(
	SELECT
		DATE_FORMAT(add_time, '%Y-%m-%d') as 'add_time', COUNT(add_time) as 'reach_intention' , '' as 'post_release'
	FROM
		table1
	where
		DATE_FORMAT(add_time, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 30 day), '%Y-%m-%d')
	group by
		DATE_FORMAT(add_time, '%Y-%m-%d')
UNION ALL
	select
		DATE_FORMAT(add_date, '%Y-%m-%d') as 'add_time' , '' as 'reach_intention' , COUNT(add_date) as 'post_release'
	from
		table2
	where
		DATE_FORMAT(add_date, '%Y-%m-%d') > DATE_FORMAT(date_sub(now(), interval 30 day), '%Y-%m-%d')
	group by
		DATE_FORMAT(add_date, '%Y-%m-%d') ) test
group by
	test.add_time
order by
	test.add_time desc

結果如下

總結

到此這篇關於MySQL兩個查詢如何合併成一個結果的文章就介紹到這了,更多相關MySQL兩個查詢合併一個內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com