just use
SELECT MAX(result, 0)
質問
I have a query that I am trying to amend to basically include another basic SELECT query within it and minus that value.
For instance my query is currently as follows:
SELECT
(SELECT COALESCE(SUM(x.points),0) - (SELECT SUM(price) FROM purchases WHERE user_id = '. $user_id .')
FROM codes x
WHERE x.user_id=5701960905 AND x.points > 0
AND x.inactive = 0 AND (x.date_redeemed >= 1252627200
OR x.date_redeemed = 0)) as totalPointsSum
FROM `codes` `t`
WHERE t.inactive = 0 AND (t.date_redeemed >= 1252627200 OR t.date_redeemed = 0)
LIMIT 1;
// This will return an integer value e.g '51'
I basically only require it to return a value of 0 or greater, if the returned result is a negative value I'll need to default the return value to 0 rather than -31 for example.
Any advice would be appreciated
解決
just use
SELECT MAX(result, 0)
他のヒント
This seem to work for me:- IF NULL (value, default_val)
IFNULL( SUM(price), 0 )