简体   繁体   中英

How do I include all months when using MONTHNAME when some months have null data in mysql database?

I have sql query

select
    DISTINCT {fn MONTHNAME(entrydate)} AS monthname,
    emirate_name,
    location,
    price,
    bedroom 
from itf_property 
where 
    emirate_name='Dubai' 
    and location='JBR' 
    and bedroom='2' 
group by monthname 
order by id

I want to select the null month also from jan to dec like if jan have no value then it will show 0.

The output of above query is attached image

在此处输入图片说明

Use if on mysql

select
DISTINCT {fn MONTHNAME(entrydate)} AS monthname,
emirate_name,
if(month is null, '0', month) as month, 
location,
price,
bedroom 
from itf_property 
where 
emirate_name='Dubai' 
and location='JBR' 
and bedroom='2' 
group by monthname 
order by id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM