0
我創建了2個表消息和對話。簡單的mysql查詢以特定的順序顯示結果
對話表將爲兩個正在發送和接收消息的用戶創建一個唯一的ID。
消息表將包含用戶消息的所有信息。
會話表
conversation_id | user_id1 | user_id2 |last_message_id
1 5 1 4
2 5 2 5
message_id | conversation_id | sender_id | message | created_time
1 1 5 MSG A 2012-06-10 00:20:04
2 1 5 MSG B 2012-06-10 00:20:21
3 1 5 MSG C 2012-06-10 00:21:09
4 1 1 MSG D 2012-06-10 00:22:23
5 2 5 MSG E 2012-06-10 00:25:16
輸出我想:
sender_id | message | created_time
5 MSG E 2012-06-10 00:25:16
1 MSG D 2012-06-10 00:22:23
我試了一下:
SELECT * FROM conversation c, messages m
WHERE c.user_id1='$userid' OR c.user_id2='$userid'
AND c.last_message_id=m.message_id
ORDER BY created_time DESC
但THI s查詢不顯示正確的結果。
我們是否需要像下面這樣的東西來分組?
SELECT * FROM TABLE
WHERE CONDITION
(SELECT MAX(created_time)
FROM TABLE
GROUP BY SOMEFEILD)
order by time desc
偉大的!有用!!.... – Jadzia