SELECT COUNT(*)AS cnt FROM info AS i LEFT JOIN user AS u ON i.grpid=u.grpid WHERE i.type=1 GROUP BYi.grpid这样查询出来的没有结果的数据不显示,想让没有结果的数据显示为0怎么做?
2 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
泻药,没看出来你这sql哪里会有空值呢?
左连接、where过滤左表字段、groupby也是左表、取count
过滤到的记录肯定count也不为0
然后说一下空值处理用ifnull(col1,0)即可
哆啦的时光机
TA贡献1779条经验 获得超6个赞
你可以使用 MYSQL IF()Function
SELECT IF(SELECT COUNT(*)AS cnt FROM info AS i LEFT JOIN user AS u ON i.grpid=u.grpid WHERE i.type=1 GROUP BY i.grpid) = 0 , "0", SELECT COUNT(*)AS cnt FROM info AS i LEFT JOIN user AS u ON i.grpid=u.grpid WHERE i.type=1 GROUP BY i.grpid) AS cnt;
以下是IF 的 Syntax
IF(condition, value_if_true, value_if_false)
添加回答
举报
0/150
提交
取消