2016-11-11 54 views
0
select sum(charge) as charges from chargelist 

如果有在chargelist沒有記錄,$query->num_rows()返回1,不爲0,運行在phpMyAdmin此查詢,結果如下MySQL查詢返回不正確的,當我使用十進制

enter image description here

請幫我。

+1

比較'選擇SUM(NULL)作爲thisThing;'to'select IFNULL(SUM(NULL),0)作爲thisThing;' – Drew

+0

你可以通過以下查詢來處理這種情況。 查詢1:(只檢查是否總和爲null,則在地方空的使用零) '選擇IFNULL(SUM(收費),0)從chargelist費' 或者 問題2:(應用情況和檢查) 'select(count(charge)> 0 then then(charge)else 0 end)時從收費表中收取費用' –

回答

1

嗯,這是ANSI SQL行爲,如SO answer所示。 如果要空車返回記錄集,你可以在外部查詢檢查

select * from (select sum(charge) as charges from chargelist) As tmp 
where charges is not null