2つのテーブルメッセージと会話を作成しました。
会話テーブルは、メッセージを送受信している2人のユーザーの一意の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
しかし、このクエリは正しい結果を表示していません。
グループ化するためにフォローするようなものも必要ですか?
SELECT * FROM TABLE
WHERE CONDITION
(SELECT MAX(created_time)
FROM TABLE
GROUP BY SOMEFEILD)
order by time desc