請參閱下面的SQL查詢,指望它是數量和否CheckDate
問題與左連接 - 匹配數
之間在MatchSales
場 - 它不工作,我怎麼想是。它應該找到(取決於CheckDate
)匹配S.mobile
D.MobileNO
(檢查所有記錄在sales
表)
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= $From_timestamp AND UNIX_TIMESTAMP(CheckDate) <= $To_timestamp THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' AND UNIX_TIMESTAMP(CheckDate) >= $From_timestamp AND UNIX_TIMESTAMP(CheckDate) <= $To_timestamp THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as MatchSales
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Company'
GROUP BY D.Username
在SUM? ..... – user622378
不,在'source ='Company'之後# – judda
掛上我會編輯以顯而易見 –