当几个月在mysql数据库中有空数据时,如何包含使用MONTHNAME的所有月份?

时间:2015-12-17 12:36:02

标签: php mysql sql

我有sql查询

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

我想从jan到dec选择null月,如果jan没有值则会显示0。

上述查询的输出是附加图像

enter image description here

1 个答案:

答案 0 :(得分:0)

在mysql上使用if

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