Forum Moderators: open

Message Too Old, No Replies

whats wrong with this query?

         

PHPycho

11:51 am on Jan 28, 2008 (gmt 0)

10+ Year Member



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

phranque

12:37 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



that looks correct to me, but you could try returning the 0 as a string expression (single quoted) instead of an integer:
"SELECT IF(COUNT(*) > 0, COUNT(*), '0') AS total FROM table1 WHERE field1 = 'some_value' GROUP BY field2"

ZydoSEO

7:11 am on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is:

SELECT IF(COUNT(*) > 0, COUNT(*), 0) AS total FROM table1 WHERE field1 = 'some_value' GROUP BY field2

not logically equivalent to:

SELECT COUNT(*) AS total FROM table1 WHERE field1 = 'some_value' GROUP BY field2