sql语句查出的数据为空,怎么用个if语句判断,然后作出处理?

如题所述

可以实现,以sql server为例看:
if not exists(select userName from food join diningcar on food.foodId=diningcar.foodId join users on diningcar.userId=users.userId where (comment=0 or comment=-1) and userName='zq' group by userName)
select 0,'zq'
else
select sum(price),userName from food join diningcar on food.foodId=diningcar.foodId join users on diningcar.userId=users.userId where (comment=0 or comment=-1) and userName='zq' group by userName

方法二:
select isnull(sum(price),0),userName
from food join diningcar on food.foodId=diningcar.foodId
join users on diningcar.userId=users.userId
where (comment=0 or comment=-1) and userName='zq'
group by userName

不知道是不是你想要的结果,但是我有个疑问,你为什么不在程序里进行判断,而是要让sql语句判断呢?
温馨提示:答案为网友推荐,仅供参考
相似回答