MySQL Forums
Forum List  »  Newbie

Re: COUNT inside IF function returning wrong result
Posted by: Peter Brawley
Date: June 22, 2015 09:08PM

Joins can make aggregate results multiply as in the Sorcerer's Apprentice. Try moving the aggregation to a subquery eg ...

select u.user_id, m.N
from users u
left join (
  select user_id, count(message_id) as N
  from messages
  group by user_id
) m using(user_id);

Options: ReplyQuote


Subject
Written By
Posted
Re: COUNT inside IF function returning wrong result
June 22, 2015 09:08PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.