假设这个
用户表:[id] [mail] [pass]
发生表:[id] [uid] [date] [content]
投票表 [uid] [hid] [type] [datetime]
1 个用户可以有 0 个或更多发生,1 个发生可以有 0 个或更多选票..
我想获得特定事件的总票数
SELECT
H.*,
SUM(CASE WHEN V.type='C' THEN 1 ELSE 0 END) AS upvotes,
SUM(CASE WHEN V.type='R' THEN 1 ELSE 0 END) AS downvotes
FROM
happens H
LEFT JOIN
votes AS V
ON V.hid = H.id
WHERE
H.uid = :uid
问题是,如果没有投票与发生相关联,我会从 mysql 得到一个空行,不是 EMPTY 而是 NULL
我怎样才能避免这个空结果?
[编辑]
一些尝试:
基本的
SELECT H.* FROM happens H LEFT JOIN votes AS V ON V.hid = H.id WHERE H.uid = '178d937'
结果->空
使用 SUM、COUNT、HAVING 等的所有其他尝试给出:
[id] [uid] [what] [latitude] [longitude] [date] [time] [hide] [upvotes] [downvotes]
NULL NULL NULL NULL NULL NULL NULL NULL 0 0