开发者

RIGHT JOIN not returning the right number of rows

开发者 https://www.devze.com 2023-04-03 17:17 出处:网络
$sql=\"SE开发者_如何学编程LECT MAX(mr.messageId) AS maxMessageId, mr.threadId FROM messages_recipients AS mr
$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?

0

精彩评论

暂无评论...
验证码 换一张
取 消