mysql - Multiple condition in a single SUMIF function -
select ifnull(sum(if(a=1 , b=2, 'correct', null)),0) table
please using such function above in query. although works want know if right way of handling multiple conditions within 1 single query in mysql. thanks
no. not correct. cannot sum string value. mysql convert number. because 'correct'
starts letter, query return 0.
i think intend:
select sum(if(a=1 , b=2, 1, 0)) correct table
note gets rid of outer ifnull()
. suggest using coalesce()
rather if()
because ansi standard functionality. however, conditional not needed.
the query can further simplified. in fact, best way write query is:
select count(*) table = 1 , b = 2;
in general, better write queries conditions in where
clause rather in conditional statements (if possible). reduces number of rows rest of query needs process.
Comments
Post a Comment