Hello,
I have a question about optimizing an SQL query and an error that occurs sometimes. Right now, I have a query similar to:
SELECT SUM( money_earned ) * 0.60 - (SELECT SUM( money_lost ) * 0.2 FROM t1) FROM t2
The query has a lot more going on, and the purpose is to calculate money earned and subtracting money lost. It works if there are rows that were answered incorrectly, but if the user has only answered questions correctly, then the subtraction value is NULL (since there are no rows which return as question answered wrong) which in turn causes the entire statement to return NULL.
I hope that explanation is clear; let me know if I am being ambiguous.
I was wondering how I should handle this query… for now I just have two queries (one calculating money earned, and one money lost, and then handling the subtraction using PHP). Is it more efficient to do everything on the database?
Thanks for your help!