我有 SQL 语句
SELECT *
FROM `assocMessages`
LEFT JOIN `messageThreads` ON `messageThreads`.`threadID` = `assocMessages`.`threadID`
LEFT JOIN `messages` ON `messageThreads`.`threadID` = `messages`.`threadID`
WHERE `assocMessages`.`accountID` =1
GROUP BY `messageThreads`.`threadID`
ORDER BY `messages`.`messageSent` DESC
它旨在获取message threads
与当前用户相关联的列表。它还旨在获取message
与 found 相关联的最新message threads
消息,但是,它仅显示在 found 上发送的第一条消息message thread
。我添加了 statement ORDER BY messages.messageSent DESC
,但是,无论它在那里,或者方向是ASC
or DESC
,它只显示第一个message
发送。有谁知道我能做什么?我试图将其保留在单个 SQL 语句中,以防止遇到 n+1 选择问题。
编辑
我刚刚尝试反转表格,所以它最初有FROM messages
并且assocMessages
表格稍后加入
编辑
关联消息:
+-----------+----------+
| accountID | threadID |
+-----------+----------+
| 1 | 1 |
+-----------+----------+
| 2 | 1 |
+-----------+----------+
消息线程:
+----------+---------------------+
| threadID | threadCreatedOn |
+----------+---------------------+
| 1 | 2013-05-01 12:00:00 |
+----------+---------------------+
留言:
+-----------+----------+-----------+-------------+---------------------+
| messageID | threadID | accountID | messageBody | messageSent |
+-----------+----------+-----------+-------------+---------------------+
| 1 | 1 | 1 | First Hi | 2013-05-01 12:00:00 |
+-----------+----------+-----------+-------------+---------------------+
| 2 | 1 | 2 | Second Hi | 2013-05-01 12:01:00 |
+-----------+----------+-----------+-------------+---------------------+
因此,当被查询时,它将返回一个线程列表(accountID
1 已注册),其中最近的 2message
是2messageID
发送的accountID
返回的内容:
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
| accountID | threadID | threadCreated | messageID | messageBody | messageSent | threadID | accountID |
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
| 1 | 1 | 2013-05-01 12:00:00 | 1 | First Hi | 2013-05-01 12:00:00 | 1 | 1 |
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
应该返回什么:
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
| accountID | threadID | threadCreated | messageID | messageBody | messageSent | threadID | accountID |
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
| 1 | 1 | 2013-05-01 12:00:00 | 2 | Second Hi | 2013-05-01 12:01:00 | 1 | 2 |
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
如果我删除返回什么group by
:
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
| accountID | threadID | threadCreated | messageID | messageBody | messageSent | threadID | accountID |
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
| 1 | 1 | 2013-05-01 12:00:00 | 2 | Second Hi | 2013-05-01 12:01:00 | 1 | 2 |
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
| 1 | 1 | 2013-05-01 12:00:00 | 1 | First Hi | 2013-05-01 12:00:00 | 1 | 1 |
+-----------+----------+---------------------+-----------+-------------+---------------------+----------+-----------+
第一个accountID
s 是相同的,因为它们是从messageThreads
关联返回的
最终编辑(暂时)
当我删除 时GROUP BY messageThreads.threadID
,它会显示每个线程中的所有消息