Adding WHERE YEAR breaks the calculation

I have a very simple query:

SELECT a, b, a+b as c FROM records 

Which works just fine. He is an example from the results:

 Array ( [a] => 100.92 [b] => 21.00 [c] => 121.92 ) 

But the minute I try to filter records by year:

 SELECT a, b, a+b as c FROM records WHERE YEAR(`mydate`) = '2011' 

The calculation of a + b disappears:

 Array ( [a] => 100.92 [b] => 21.00 [c] => ) 

Am I missing something obvious?

Update:

  • I was asked to specify the actual SQL, here it is:

    CHOOSE a loan, debit, loan + debit as the final transaction WHERE YEAR ( transaction_date ) = '2011'

  • A transaction type is a DATE type.

  • In addition, either the credit or debit field is always zero.

  • PHP code:

code

 include($_SERVER["DOCUMENT_ROOT"] . "/pool/_/db.php"); $sql = "SELECT credit, debit, credit+debit as total FROM transactions WHERE YEAR(`transaction_date`) = 2011"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { print_r($row); } 
+4
source share
1 answer

The only thing I can think of is that either credit or debit is NULL (not 0 ), which in turn will also make total NULL . Try this and see if this has changed:

 ... COALESCE(credit, 0) + COALESCE(debit, 0) as total ... 
+3
source

Source: https://habr.com/ru/post/1434686/


All Articles