没有适用的行时,COUNT(*)返回false结果

时间:2013-09-10 08:34:24

标签: mysql sql count

我正在运行以下查询:

SELECT *, COUNT(iup.user_id) AS user_plans_count
FROM #__iop_user_plans iup
INNER JOIN #__iop_plan ip ON iup.iop_id = ip.id
WHERE `iup`.`state` = 1 AND `iup`.`user_id` IS NOT NULL AND `iup`.`accepted` = 0

当没有适用的行时,MySQL仍会返回一个仅包含计数的结果。如何将查询更改为仅在大于0的情况下返回计数?

1 个答案:

答案 0 :(得分:2)

这将完成工作

SELECT *, COUNT(iup.user_id) AS user_plans_count
FROM #__iop_user_plans iup
INNER JOIN #__iop_plan ip ON iup.iop_id = ip.id
WHERE `iup`.`state` = 1 AND `iup`.`user_id` IS NOT NULL 
AND `iup`.`accepted` = 0 
HAVING user_plans_count>0