I have a like the one below that does a minus from another select. The problem I have is that if the second SELECT (the one to do minus with) returns NULL, the full query returns NULL even if the first query has values. Seems like MySQL thinks 1-NULL=NULL. How can I fix this?
SELECT round(sum(iv.`amount`)) -
(
SELECT round(sum(pay.`amount`)) amountSum
FROM invoice iv
LEFT JOIN invoiceFactoring ivf on ivf.invoiceID=iv.invoiceID
LEFT JOIN user systemuser ON (systemuser.userID=iv.ownerUserID)
LEFT JOIN Payment pay ON (pay.`invoiceID`=iv.`invoiceID`)
WHERE
(iv.invoiceStateID = 2 OR iv.invoiceStateID = 3)
AND
(ivf.`invoiceFactoringProcessID` = 7)
AND (pay.`paymentMethodID` = 1 OR pay.`paymentMethodID` = 2)
AND systemuser.`groupID` = 1
AND iv.`disabled` <> 1
AND ivf.`invoiceExpiryDate` BETWEEN date_add(now(), INTERVAL - 28 DAY) AND date_add(now(), INTERVAL - 21 DAY)
)
FROM invoice iv
LEFT JOIN invoiceFactoring ivf on ivf.invoiceID=iv.invoiceID
LEFT JOIN user systemuser ON (systemuser.userID=iv.ownerUserID)
WHERE
(iv.invoiceStateID = 2 OR iv.invoiceStateID = 3)
AND
(ivf.`invoiceFactoringProcessID` = 7 or ivf.`invoiceFactoringProcessID`)
AND systemuser.`groupID` = 1
AND iv.`disabled` <> 1 /* ta bort de som är inaktiva*/
AND ivf.`invoiceExpiryDate` BETWEEN date_add(now(), INTERVAL - 28 DAY) AND date_add(now(), INTERVAL - 21 DAY)
1-0=0can be considered a universal truth but I don't think the same can be said of1-NULL=NULL. Rather, it is something defined** by the SQL Standard and implemented by mySQL. ** I mean that loosely!