Question

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

Was it helpful?

Solution

just use

SELECT MAX(result, 0)

OTHER TIPS

This seem to work for me:- IF NULL (value, default_val)

IFNULL( SUM(price), 0 )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top