2014-04-12 67 views
0

這是我的表:Mysql的集團通過功能

Id customer email  timestamp    store_id 
1 1  [email protected] 2014-04-11 17:06:21 5416 
2 0  [email protected] 2014-04-11 15:05:23 5416 
3 1  [email protected] 2014-04-11 17:55:34 5416 
4 2  [email protected] 2014-04-11 17:55:34 5416 

和我的查詢是:

SELECT *, count(*) as total FROM `tybo_orders` 
WHERE timestamp IN (SELECT max(timestamp) FROM `tybo_orders` 
GROUP BY email) AND store_id='5416' GROUP BY email 

0在customer列中的值表示訪客和超過0意味着客戶ID。如果customer爲0,則我想通過email進行分組,其他分組由customer進行分組。這怎麼可能?

+0

使用case語句適用condtions – Divya

+0

什麼是你想要的輸出? – wils484

回答

0

嘗試像這樣

SELECT *, count(*) as total 
FROM `tybo_orders` 
WHERE timestamp IN (SELECT max(timestamp) FROM `tybo_orders` GROUP BY email) 
AND 
store_id='5416' 
GROUP BY 
case when customer=0 then customer else email end 
0

,我認爲這會工作:

SELECT *, count(*) as total 
FROM `tybo_orders` 
WHERE timestamp IN (SELECT max(timestamp) FROM `tybo_orders` GROUP BY email) 
AND 
store_id='5416' 
GROUP BY 
IF(customer=0,email,customer)