如果mysql中不存在数据,如何添加总计为0的月份名称?
我需要获取用户从创建日期到当前日期的数据,按月份名称和年份以及每个月的总价 尝试1: 输出:如果mysql中不存在数据,如何添加总计为0的月份名称?,mysql,sql,Mysql,Sql,我需要获取用户从创建日期到当前日期的数据,按月份名称和年份以及每个月的总价 尝试1: 输出: month year total July 2019 360 September 2019 2160 October 2019 360 预期产出: All month name and total will be 0 if data not exist. month year t
month year total
July 2019 360
September 2019 2160
October 2019 360
预期产出:
All month name and total will be 0 if data not exist.
month year total
Jan 2018 0
Feb 2018 0
...
Dec 2018 0
Jan 2019 0
Feb 2019 0
Mar 2019 0
Apr 2019 0
May 2019 0
Jun 2019 0
July 2019 360
Aug 2019 0
Sep 2019 2160
Oct 2019 360
Nov 2019 0
Dec 2019 0
经过一些RND,我找到了一种方法,我也尝试过,但现在起作用了
尝试2:
这是我的链接
有人能帮我解决这个问题吗
提前谢谢 如果表中有所有月份的数据,但没有该id的数据,则可以切换到条件聚合:
SELECT MONTHNAME(start_time) as month, YEAR(start_time) as year,
SUM(CASE WHEN t1.id = 33 THEN price ELSE 0 END) as total
FROM table t1
GROUP BY YEAR(start_time),MONTHNAME(start_time)
ORDER BY MIN(start_time);
继续使用LEFT JOIN,但只使用一次整数生成器(最多12个),而不是月份名称,在ORDER BY子句中也很容易使用,因为您可以通过monthname函数获取月份名称。根据你的第二个问题,我考虑了今年。因此,请使用:
SET @year=YEAR(NOW());
SELECT
MONTHNAME(STR_TO_DATE(concat(YEAR(NOW()),',',m.month,',1'),"%Y,%m,%d")) as month,
@year as year , SUM( COALESCE( pu.price, 0) ) as total
FROM (
SELECT rn as month
FROM
(
SELECT @rn := if(@i = @rn, @rn + 1, 1) as rn,
@i := @i+1
FROM information_schema.character_sets
JOIN (SELECT @i := 0, @rn := 0) as q_iter
LIMIT 12
) q
) AS m
LEFT JOIN table_u pu ON MONTH(pu.start_time) = m.month
AND YEAR(pu.start_time) = @year
AND ID = 33
GROUP BY m.month
ORDER by m.month;
2018年初至本年末编辑:
使用用户变量创建不存在的年-月对:
SELECT monthname(str_to_date(concat_ws(',',ym.month,'01,01'),'%m,%d,%y')) month
, ym.year year
, sum(price)
FROM table1 t1
RIGHT JOIN( SELECT @year := if(@month=12, @year+1, @year ) year
, @month := if(@month=12, 1 , @month+1) month
FROM table1
, ( SELECT @startYear := min(start_time)
, @endYear := year(now())
, @month := 12
, @year := min(start_time)-1
FROM table1
) t
WHERE (@year,@month) < (@endYear,12)
) ym
ON ym.year = year(t1.start_time)
AND ym.month = month(t1.start_time)
GROUP BY year(t1.start_time)
, month(t1.start_time)
ym派生表将提供从表1中最小年份开始到当前年份的年-月对。
最内层的选择用于变量初始化。您可以尝试以下操作:
SELECT to_char(start_time,'month')MONTH,to_char(start_time,'yyyy')YEAR,SUM(price)total
FROM TABLE_NAME
GROUP BY to_char(start_time,'month'),to_char(start_time,'yyyy')
最后,我得到了我想要的正确输出
select
DATE_FORMAT(m1, '%M - %Y')
from
(
select
('2013-07-23')
+INTERVAL m MONTH as m1
from
(
select @rownum:=@rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select @rownum:=-1) t0
) d1
) d2
where m1<=NOW()
order by m1
谢谢你的回答。在我的情况下,我不可能在一年中为特定用户指定月份名称。例如,他有3个月没有工作了,然后我需要给他显示相应月份的0。@Sachinsah。您没有抓住要点,显然没有运行查询。如果任何id的数据中都有月份,则使用此查询的结果集中会有月份。数据仅为33。好的,我稍后会尝试并让您知道。我已经检查过了,今年的效果很好。我只需要再升一级。例如,我有一个日期2018-10-10,然后我需要从2010年的月份到当前月份的数据。我从未想过我可以运行这种类型的查询。也许这需要一些时间来理解它是如何工作的好的,我修复了Sachinsah。谢谢BarbarosÖzhan。我会查一查让你知道的。谢谢你的回复。这对我不起作用。我得到了这个输出。月年合计价格一月零360一月零2160一月零360你能检查我的预期输出吗谢谢你的回复。我会检查并让您知道。to_char是默认函数?是的!to_char是默认函数。“用于日期”将转换为字符串。有关更多信息,请参阅下面的链接。好的,我会再试一次并让您知道。函数TO_CHAR不存在
SELECT monthname(str_to_date(concat_ws(',',ym.month,'01,01'),'%m,%d,%y')) month
, ym.year year
, sum(price)
FROM table1 t1
RIGHT JOIN( SELECT @year := if(@month=12, @year+1, @year ) year
, @month := if(@month=12, 1 , @month+1) month
FROM table1
, ( SELECT @startYear := min(start_time)
, @endYear := year(now())
, @month := 12
, @year := min(start_time)-1
FROM table1
) t
WHERE (@year,@month) < (@endYear,12)
) ym
ON ym.year = year(t1.start_time)
AND ym.month = month(t1.start_time)
GROUP BY year(t1.start_time)
, month(t1.start_time)
SELECT to_char(start_time,'month')MONTH,to_char(start_time,'yyyy')YEAR,SUM(price)total
FROM TABLE_NAME
GROUP BY to_char(start_time,'month'),to_char(start_time,'yyyy')
select
DATE_FORMAT(m1, '%M - %Y')
from
(
select
('2013-07-23')
+INTERVAL m MONTH as m1
from
(
select @rownum:=@rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select @rownum:=-1) t0
) d1
) d2
where m1<=NOW()
order by m1