Reputation: 1765
I have 3 tables users(name, email), reviews(review, entity, user) and votes(vote, entity, user)
, I want to make a query which returns all the reviews given for a particular entity, and vote from votes table if the user have voted for that entity, else null should be there at the place of vote. The query I wrote is
select users.name as name,
reviews.review as review,
votes.vote as vote
from
users join reviews on users.email=reviews.user
left join votes on users.email=votes.user
where reviews.entity='entity_id'
But this query is resulting multiple rows for some reviews, I made it working using group by, by am unable to understand the behavior of left join,
Upvotes: 0
Views: 122
Reputation: 324
You are not joining votes for any specific entity, try the following:
select users.name as name,
reviews.review as review,
votes.vote as vote
from
users join reviews on users.email=reviews.user
left join votes on users.email=votes.user and reviews.entity = votes.entity
where reviews.entity='entity_id'
Upvotes: 1