gpt4 book ai didi

php - 避免查询的空结果

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:59 25 4
gpt4 key购买 nike

假设这个

用户表:[id] [邮件] [密码]

发生表:[id] [uid] [日期] [内容]

投票表[uid] [hid] [类型] [日期时间]

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

最佳答案

使用HAVING检查空值

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
HAVING upvotes IS NOT NULL

关于php - 避免查询的空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18961826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com