Reputation: 134453
I need to add a join to the following query that will join the messages_message table to the messages_threads table and return the 'body' and 'time' fields from the most recent row in messages_messages where messages_message.threadid = messages_threads.id.
Sorry that's so confusing, I don't know how else to phrase it! Here's my current query - which pulls everything correctly except the 'body' and 'time' fields.
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
Upvotes: 0
Views: 70
Reputation: 3972
You could add a subquery of the messages_message table which would return 1 row for every thread. For example:
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
Upvotes: 3
Reputation: 2401
I think you will need Group by body and time also... or can u more specify your query..
Upvotes: 1