我正在思考做我想要完成的事情的最佳方式。正如标题所说,我在mysql中有一个缺少日期的表,如下所示:
+------------+
| 2015-09-01 |
| 2015-09-03 |
| 2015-09-05 |
| 2015-09-06 |
| 2015-09-07 |
+------------+
我想选择它们为其分配另一个null
值的列,所以如果我还想选择缺少的日期(在我的情况下是第2天和第4天),哪些是我的选择?我想过做一个"日历"使用左边连接并使用not in
选择表格,但这意味着我必须填写TON日期(如果有人出于某种原因想要选择2500或1800年的数据) ,我不想要那个。)
我不喜欢使用日历表的选项,因为它有性能,但它还带来了另一个问题:如果我仍然使用日历表,并限制人们在过去或将来选择日期(因此,让我不必投入数百年的保证金,以防一些疯狂的灵魂决定从那里选择东西),我怎么能让表填充自己所以它有一个当前日期的日期,而我不必插入当前手动约会?
触发器/功能可以每天运行并在那里插入当前日期吗?
编辑:我的目的是将表格用于chartjs,因此它可以有" hole"缺少数据的地方(空值)。
答案 0 :(得分:1)
检查一下:
select this_date,given_dates from
(SELECT ADDDATE('2015-09-01', INTERVAL @i:=@i+1 DAY) AS this_date
FROM (
SELECT a.a
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
) a
JOIN (SELECT @i := -1) r1
WHERE
@i < DATEDIFF('2018-01-30', '2015-09-01')) as a
left JOIN
(select given_dates from
(select '2015-09-01' as given_dates union all
select '2015-09-03' union all
select '2015-09-05' union all
select '2015-09-06' union all
select '2015-09-07') as a) as b
on a.this_date = b.given_dates
样本结果:
2015-09-01 2015-09-01
2015-09-02
2015-09-03 2015-09-03
2015-09-04
2015-09-05 2015-09-05
2015-09-06 2015-09-06
2015-09-07 2015-09-07
2015-09-08
2015-09-09
2015-09-10
2015-09-11
您可以轻松获得它的空值。