mysql - Return null rows for non-matched values in IN clause in SQL -
i have query returns records if found values in in clause. matched records vary 0 1 , result of query. want make count 5, if rest rows filled null. there way can achieve this? as, given query
select categoryid, categoryname category categoryid in ( 52, 58, 60 , 62 , 64)
i have values first 3 ids , result gives me 3 rows only. want count of result 5 , rest non-satisfying rows filled null values make count. current result:
categoryid categoryname 52 abc 58 xyz 60 def
the required result
categoryid categoryname 52 abc 58 xyz 60 def null null null null
use left join
synthesized table lists category ids want:
select c.categoryid, c.categoryname (select 52 categoryid union select 58 union select 60 union select 62 union select 64) left join category c on a.categoryid = c.categoryid
another way create table contains possible category ids. use:
select c.categoryid, c.categoryname allcategories left join category c on a.categoryid = c.categoryid a.categoryid in (52, 58, 60 , 62 , 64)
Comments
Post a Comment