我試圖返回兩個用戶之間的第一條消息,無論我是發送該消息還是收到它。我編寫了下面的查詢,它返回所有正確的線程,但似乎忽略了DISTINCT子句 - 最後我得到了多個具有相同'someid'的結果。SELECT DISTINCT在CASE語句後不工作
如何糾正此查詢,以便在結果'someid'列中不會有重複值返回?
這裏就是我目前使用:
SELECT DISTINCT CASE WHEN $userid != senderid THEN senderid ELSE GROUP_CONCAT(receivers.id SEPARATOR ', ') END someid,
CASE WHEN $userid != senderid THEN senders.username ELSE GROUP_CONCAT(receivers.username SEPARATOR ', ') END somename,
messages.body,
messages.time
FROM messages
LEFT JOIN messages_recipients AS recipients ON messages.id = recipients.messageid
LEFT JOIN users AS senders ON messages.senderid = senders.id
LEFT JOIN users AS receivers ON recipients.userid = receivers.id
WHERE recipients.userid = $userid
OR messages.senderid = $userid
GROUP BY messages.id
ORDER BY messages.time DESC
我想你將不得不闖入2個查詢,內部執行SomeId上的Distinct ... – 2011-04-13 00:10:23
我會在哪裏放置該查詢? – Walker 2011-04-13 01:28:00