n3mesis125 Posted November 13, 2008 Share Posted November 13, 2008 Hey FOlks, How can I fix an error so that if I'm using SUM() with two tables and lets say Table B doesn't return any values for the WHERE clause? IE: SELECT SUM(t1.salary) as t1total, SUM(t2.salary) as t2total FROM table1 as t1 LEFT JOIN table2 as t2 ON (t2.id = t1.id) WHERE t1.date BETWEEN '2008-10-01' AND '2008-10-20' AND t1.username = 'test.account' GROUP BY t1.date Table 2 may not return any data if the user we are looking for hasn't submitted anything. For some reason mysql doesn't return any data at all when it can't SUM() an empty resultset? Hope this makes sense, I tried using IFNULL(t2.salary,0) but it doesn't seem to work. Thanks, n3m. Quote Link to comment https://forums.phpfreaks.com/topic/132611-sum-help/ Share on other sites More sharing options...
fenway Posted November 13, 2008 Share Posted November 13, 2008 For some reason mysql doesn't return any data at all when it can't SUM() an empty resultset? As per the manual: "If the return set has no rows, SUM() returns NULL." Try: SUM( IFNULL( t2.salary, 0 ) ) as t2total Quote Link to comment https://forums.phpfreaks.com/topic/132611-sum-help/#findComment-689636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.