mysql - COALESCE and IFNULL not working as expected -
i'm trying output default row when there's no row found in query. sample of query:
select coalesce(site, 'ste') site, instrument, field table1 site in ('east', 'west') , date(tstamp) = "2016-09-07" order id desc
the output is
+------+------------+-------+ | site | instrument | field | +------+------------+-------+ | west | 0 | 0 | +------+------------+-------+
for tsamp 2016-09-07 have row site "west" , there's no row "east". tried search , found can use coalesce , tried ifnull i'm getting output above. tried if(count(site) = 0, "ste", site) can't work.
my expected result
+------+------------+-------+ | site | instrument | field | +------+------------+-------+ | west | 0 | 0 | | ste | null | null | +------+------------+-------+
i hope guys can me. in advance
both coalesce() , ifnull() work on row basis, meaning can replace null value if null value exists in record. however, cannot create record not exist - , not have records matching east (or ste).
a possible solution create table has possible values site
field , can left join on table:
select coalesce(table1.site, "ste") site, table1.instrument, table1.field lookuptable lt left join table1 on lt.site=table1.site lt.site in ('east', 'west') , date(table1.tstamp) = "2016-09-07" order id desc
Comments
Post a Comment