php - 当 mysql 数据库中某些月份有空数据时,如何在使用 MONTHNAME 时包含所有月份?

标签 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

我想选择从 1 月到 12 月的空月份,就像如果 1 月没有值那么它将显示 0。

上述查询的输出附有图片

enter image description here

最佳答案

如果在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

关于php - 当 mysql 数据库中某些月份有空数据时,如何在使用 MONTHNAME 时包含所有月份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334778/

相关文章:

PHP MySQL 注入(inject)安全 : Does verifying that $_GET input is numeric provide enough protection?

php - 在 Laravel 5.2.37 中将信息从一个浏览器发送到另一个浏览器

php - 通过PHP更新查询清算结果

SQL - 如果满足使用多个先前列的条件,则滞后获取先前的值

python - 在 Python 中连接两个 SQL 字符串列表

sql - 使用 SQL Server 临时表的最佳实践

PHP 搜索显示与查询完全不同的重复结果

php - Laravel Eloquent : How to order results of related models?

php - 如何存储重复日期牢记夏令时

PHP 删除按钮...不起作用(模态)