I was trying to add sum of Total value from different tables. It's sucess with this query But I want addition of current date.
Table1 Table2
Date Total_Cost Date Total_Cost
21/01/2015 500 21/01/2015 500
SELECT (SELECT SUM(Total_Cost) FROM Table1) + (SELECT SUM(Total_Cost) FROM Table2) as total
Its Give proper answer : 1000
But I Run this Query Addition With Current Date.
SELECT (SELECT SUM(Total_Cost) FROM Table1 WHERE DATE = CURDATE()) + (SELECT SUM(Total_Cost) FROM Table2 WHERE DATE = CURDATE()) as total
Its Give Value :NULL
Please help me someone To solve this thank you in advance.
Your query needs to cope with the possibility that no rows may be returned by either of your select statements (ie a NULL value) by using ISNULL eg
ISNULL(SELECT SUM(X) FROM Table1 WHERE Date=CURDATE(),0.0)
or even:
SELECT ISNULL(SUM(Total_Cost),0.0) FROM
(
SELECT Total_Cost FROM Table1 WHERE Date = CURDATE()
UNION ALL
SELECT Total_Cost FROM Table2 WHERE Date = CURDATE()
) S1
Apologies if this is not valid MySQL syntax I'm more familiar with SQL Server, but I hope it helps even so.