は、これが私のテーブル構造である:私は(特定のユーザーのための)各会話について(日時に基づいて)最新のエントリを取得したいSQLクエリ/最新のエントリを取得
[id] [senderid] [recipientid] [message] [datetime]
、結果がでなければなりません(自分のユーザーID 1):
435 | 1 | 67 | how are u? | timestampnow
次の結果(レスポンス後):
436 | 67 | 1 | fine thanks | timestamplater
混乱して行う方法Q /正しく/結合してください。
SELECT a.[id], a.[senderid], a.[recipientid], a.[message], a.[datetime]
FROM yourTable a
INNER JOIN (SELECT recipientid, MAX(date) mdate
FROM yourTable
GROUP BY recipientid) b ON b.recipientid = a.recipientid
AND a.datetime = b.mdate;
http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple- sql-query – Strawberry