我有一个表来评估评论,并且喜欢一个用户:
http://sqlfiddle.com/#!9/5cda66/1
有3个喜欢和6条评论。但它显示3个喜欢和18个评论。它总是成倍增加。 3 x 6 = 18,这是错误的。
如何解决这个问题?
select
count(DISTINCT likes.user) as likes_count,
SUM(CASE comentarios.delete WHEN 0 THEN 1 ELSE 0 END) as comentarios_count
from posts p
left join likes on likes.post = p.id
left join comentarios on comentarios.foto = p.id
group by p.id
答案 0 :(得分:2)
如果您将条件放在if ([resultBinary integerValue] == 0) {
...
} else {
...
}
子句
count(distinct)
left join