Click to See Complete Forum and Search --> : whats wrong with this query ??


PHPycho
01-28-2008, 06:51 AM
Hello forums !!
I am getting problems in this query :
"SELECT IF(COUNT(*) > 0, COUNT(*), 0) AS total FROM table1 WHERE field1 = 'some_value' GROUP BY field2"
if there is no result the query is supposed to give 0 value but it gives nothing.
Can anbody help what went wrong in above query ?
thanks in advance to all of you

chazzy
01-28-2008, 07:31 AM
it's partially because your premise is wrong.

Your query assumes that

SELECT COUNT(*) FROM table1 WHERE field1 = 'some_value' GROUP BY field2
Returns 0 if there are no occurrences of same_value in field1. This is untrue. It will return the empty set. Try using a null check instead of if, it might work.