简体   繁体   中英

Combine results of two SQL queries

I want to combine the results of two queries into one resulttable with three columns, result1 , result2 and dateday , the queries are contradictory

SELECT COUNT( DISTINCT `cust` ) AS result1, DATE( `date` ) AS dateday
FROM `salg_test`
WHERE `saved` =0
AND `is_void` =0
GROUP BY dateday
ORDER BY dateday DESC

SELECT COUNT( DISTINCT `cust` ) AS result2, DATE( `date` ) AS dateday
FROM `salg_test`
WHERE `saved` =1
AND `is_void` =0
GROUP BY dateday
ORDER BY dateday DESC

I want both results to be grouped by and ordered by the dateday variable.

Should be able to do something like this:

SELECT COUNT( DISTINCT CASE `saved` when 0 then `cust` else null end ) AS result1
, COUNT( DISTINCT CASE `saved` when 1 then `cust` else null end ) AS result2
, DATE( `date` ) AS dateday
FROM `salg_test`
WHERE `is_void` =0
GROUP BY dateday
ORDER BY dateday DESC

how about:

WHERE (`saved`= 1 OR `saved`= 0)

Or, if the only possible values of saved are 1 or 0, just omit that condition from the query altogether.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM