我有以下表SQL GROUP BY和MAX()不會得到正確的結果
mes_id | user_a | user_b | message | room | time
------------------------------------------------------
1 | 23 | 24 | hello | 1 | 5676766767
user_a
在room
1
發送message
爲user_b
我想要得到的所有的最後爲每個房間的特定用戶(例如user_b)收到的消息,我嘗試了幾個查詢,但沒有找到正確的。
該解決方案並沒有爲我工作:sql_group_by_max
更新
我使用5.5.18 MySQL服務器
確定
這給我的結果,感謝
SELECT *
FROM messeges C
JOIN (
SELECT room, user_a, MAX(messeges.date) AS max_time
FROM messeges
GROUP BY room, user_a
)a
ON a.room = c.room
AND a.user_a = c.user_a
AND a.max_time = c.date
WHERE c.user_b = '396'
你可以添加幾個疑問? – hims056 2012-08-06 08:04:59
請添加您嘗試的查詢。我們更容易找出它出錯的地方。 – Bazzz 2012-08-06 08:06:39
你正在使用什麼數據庫系統? – 2012-08-06 08:14:37