$sql="SE开发者_如何学编程LECT MAX(mr.messageId) AS maxMessageId, mr.threadId
FROM messages_recipients AS mr
RIGHT JOIN thread_recipients AS tr ON tr.threadId=mr.threadId
WHERE mr.recipientUserId='2'
GROUP BY mr.threadId";
If the above SELECT should find a row in message_recipients with a threadId of 1 and thread_recipients has multiple rows with threadId=1.. then because I have a RIGHT JOIN, I would expect it to return as many rows as the number of rows in thread_recipients where threadId=1
It is however only returning one row regardless... Can you tell me why it is doing this?
Right Join means that all rows in the joined table will be returned, with any non-matched rows in the right table filled with nulls in the left table.
I see no reason why you are not using an inner join instead?
The reason you are returning only one value is that you are grouping by mr.threadid. If all the rows has a threadid = 1 then the group by will group them all into one row.
Am i misunderstanding the question?
精彩评论