如何在mysql5存储过程中检查查询结果是否为空?
我正在使用子查询来计算一些子类别计数值。但有时它没有任何计数值。在这种情况下,我的查询将字段值更新为空。相反,我需要将其更新为零。如何检查子查询结果是否为 null 或不在 sp 内?
我尝试了这样的方法,但它不起作用。
SELECT
if(sum(sub_category),sum(sub_category),0)
FROM myobjectcount
WHERE 1
GROUP BY category_id
I am using a subquery to count some sub category count values. But some times it does not have any count value. At that case my quesry update field value as null. Instead of that I need to update it as zero. How can I check whether the subquery result is null or not not inside sp?
I tried like this but it is not working.
SELECT
if(sum(sub_category),sum(sub_category),0)
FROM myobjectcount
WHERE 1
GROUP BY category_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IFNULL() 控制流函数是一个非常方便的MySQL函数。有助于避免编写讨厌的 CASE 语句!
另外 -
where 1
谓词相当不寻常。这对我来说似乎是多余的,所以我把它省略了。The IFNULL() control flow function is a very handy MySQL function. Tends to help avoid writing nasty CASE statements!
Also - the
where 1
predicate is rather unusual. It seems superfluous to me so I have left it out.