我需要在以下查询中添加一个连接,该连接将messages_message表连接到messages_threads表,并从messages_messages中最新的行返回“body”和“time”字段,其中messages_message.threadid = messages_threads.id。
很抱歉这太令人困惑了,我不知道怎么说出来!这是我当前的查询 - 除了'body'和'time'字段之外,它正确地提取所有内容。
SELECT
messages_threads.id AS threadid, messages_threads.name AS recipientsname,
senderid, username AS sendername, pictures.url AS senderpicture
FROM
messages_recipients
JOIN messages_threads
ON messages_threads.id = messages_recipients.threadid
JOIN users
ON users.id = messages_threads.senderid
JOIN pictures
ON messages_threads.senderid = pictures.userid
AND pictures.profile = 1
WHERE messages_recipients.userid = $userid
答案 0 :(得分:3)
您可以添加messages_message表的子查询,该子查询将为每个线程返回1行。例如:
SELECT
messages_threads.id AS threadid, messages_threads.name AS recipientsname,
senderid, username AS sendername, pictures.url AS senderpicture
FROM
messages_recipients
JOIN messages_threads ON messages_threads.id = messages_recipients.threadid
JOIN users ON users.id = messages_threads.senderid
JOIN pictures ON messages_threads.senderid = pictures.userid AND pictures.profile = 1
JOIN ( SELECT threadid, MAX(id) AS postid FROM messages_message GROUP BY threadid ) t ON message_threads.id = t.threadid
JOIN messages_message ON t.threadid = messages_message.threadid AND t.postid = messages_message.id
WHERE messages_recipients.userid = $userid
答案 1 :(得分:1)
我认为你还需要按身体和时间分组...... 或者你可以更多地指定你的查询..