gpt4 book ai didi

mysql - 填充空日期

转载 作者:行者123 更新时间:2023-11-28 23:52:58 25 4
gpt4 key购买 nike

我的数据库表结构是这样的

stats_id stats_date   stats_visitors stats_visits
1 2015-08-01 10 20
2 2015-08-03 12 21
3 2015-08-04 14 24
4 2015-08-07 15 21

添加空统计日期的最简单方法是什么。例如我缺少 2015-08-02 所以它应该是:

stats_id stats_date   stats_visitors stats_visits
1 2015-08-01 10 20
5 2015-08-02 0 0
2 2015-08-03 12 21
3 2015-08-04 14 24
6 2015-08-05 0 0
7 2015-08-06 0 0
4 2015-08-07 15 21

我可以每天检查它是否有数据并填充,但是当我有超过 10K 行时,这并不是一个好主意。如果我每天使用支票,我将生成超过 10k 个查询。

有没有简单的方法可以做到这一点?

最佳答案

这应该可以帮到您。

insert into Table1 select '',selected_date,'0','0' from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between (select min(stats_date) from Table1) and (select max(stats_date) from Table1) and selected_date not in (select stats_date from Table1);

查询的 select adddate 部分将为您提供数据集最大和最小日期之间的所有日期,使用 not in 我们只会得到那些您的数据集中不存在的日期。

因为你有自动递增的 id 字段,你可以在你的选择查询中传递一个空字符串,它会忽略它并插入自动递增的值。

选择所有日期查询的来源:How to get list of dates between two dates in mysql select query

希望这能解决您的问题。

关于mysql - 填充空日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32305702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com