我有3个表users(name, email), reviews(review, entity, user) and votes(vote, entity, user)
,我想查询返回给定特定实体的所有评论,如果用户已投票给该实体则从投票表投票,否则null应该在那里投票地点。我写的查询是
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'
但是这个查询导致了一些评论的多行, 我使用group by工作,因为我无法理解左连接的行为,
答案 0 :(得分:1)
您没有加入任何特定实体的投票,请尝试以下方法:
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'