IFNULL with IN statement with mysql -
IFNULL with IN statement with mysql -
i newbie mysql....it may dump question....but have been trying 3 hours...here trying do....
select merchant_id, ifnull(count(subscribe_id),0) subscribe_table merchant_id null or merchant_id in(1000000000066,1000000000104,1000000000103,1000000000105) grouping merchant_id order find_in_set(merchant_id,'1000000000066,1000000000104,1000000000103,1000000000105'); and output is...
+------------------+---------------------------------+ | merchant_id | ifnull(count(subscribe_id),0) | +------------------+---------------------------------+ | 1000000000066 | 2 | | 1000000000103 | 1 | +------------------+---------------------------------+ but expecting in next manner...
+------------------+---------------------------------+ | merchant_id | ifnull(count(subscribe_id),0) | +------------------+---------------------------------+ | 1000000000066 | 2 | | 1000000000104 | 0 | | 1000000000103 | 1 | | 1000000000105 | 0 | +------------------+---------------------------------+ i tried adding merchant_id null... not able result default value... :(
you records in subscribe_table. if want records ids, have "create temporary table" (or utilize subquery union in thise case) values first, , bring together results it.
your query this:
select merchant_id, count(subscribe_id) (select 1000000000066 merchant_id, 1 sortkey union select 1000000000104 merchant_id, 2 sortkey union select 1000000000103 merchant_id, 3 sortkey union select 1000000000105 merchant_id, 4 sortkey ) temp left bring together subscribe_table using (merchant_id) grouping merchant_id order sortkey asc i replaced find_in_set column sortkey in subquery. count count non-null rows , homecoming 0 if none found. don't need ifnull around it.
if have more 4 merchant_ids might want doing same thing temporary table. see here examples: mysql: create inline table within select statement?
mysql
Comments
Post a Comment