sql 语句查出的数据为空,怎么用个if语句判断,然后作出处理。
发布网友
发布时间:2022-04-10 19:59
我来回答
共2个回答
热心网友
时间:2022-04-10 21:28
oracle:改为
select nvl(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
sqlserver改为
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
热心网友
时间:2022-04-10 22:46
可以实现,以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语句判断呢?