开发者

mysql: conditional statement problem, greather then

开发者 https://www.devze.com 2023-01-14 02:31 出处:网络
When I run this query It w开发者_如何转开发orks SELECT sum( amount ) AS balance FROM balance WHERE amount >= 100

When I run this query It w开发者_如何转开发orks

SELECT sum( amount ) AS balance FROM balance WHERE amount >= 100

but when I want to filter for the userid it returns NULL

SELECT sum( amount ) AS balance FROM balance WHERE amount >= 100 AND userid=4


It will return NULL if there are no rows. If you want zero instead then use this:

SELECT IFNULL(SUM(amount), 0) AS balance
FROM balance
WHERE amount >= 100 AND userid = 4

If you believe that the answer should be something other than 0 or NULL then I suggest you run this query to double-check that at least one row is returned and that the data is correct:

SELECT * 
FROM balance
WHERE amount >= 100 AND userid = 4
0

精彩评论

暂无评论...
验证码 换一张
取 消