【问题标题】:sql get all dates between two table datessql获取两个表日期之间的所有日期
【发布时间】:2017-06-12 12:05:31
【问题描述】:

另一个 SQL 挑战!

我想编写一个 MySQL 查询,在一条记录中获取两个日期之间的所有天数。

opening_times

id   | begin      | end
1    | 10:00:00   | 17:00:00
2    | 10:00:00   | 18:00:00
3    | 10:00:00   | 19:00:00

opening_periods

id   | opening_time_id  | begin       | end         
1    | 3                | 2016-03-26  | 2016-03-28
2    | 2                | 2016-03-29  | 2016-04-01  
3    | 1                | 2016-04-02  | 2016-04-03  

我想要这个输出:

date        | begin    | end
2016-03-26  | 10:00:00 | 19:00:00
2016-03-27  | 10:00:00 | 19:00:00
2016-03-28  | 10:00:00 | 19:00:00
2016-03-29  | 10:00:00 | 18:00:00
2016-03-30  | 10:00:00 | 18:00:00
2016-03-31  | 10:00:00 | 18:00:00
2016-04-01  | 10:00:00 | 18:00:00
2016-04-02  | 10:00:00 | 17:00:00
2016-04-03  | 10:00:00 | 17:00:00

我应该为此使用子查询吗? 感谢您为我指明了正确的方向!

【问题讨论】:

  • 请注意:没有“PHPMyAdmin SQL”。这是MySQL。 PHPMyAdmin 只是可以让您在 MySQL 服务器上执行查询的众多工具之一。
  • 你使用什么编程语言?
  • @ sumit:PHP 是编程语言。 @GolezTrol:谢谢指正:)
  • 考虑处理应用级代码中数据显示的问题

标签: mysql sql date between


【解决方案1】:

你可以在 mysql 中通过复杂的查询来解决它。 首先,您需要构建一个将生成整数序列的子查询,如this answer

SELECT @row := @row + 1 as rown FROM 
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t1,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, 
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3, 
(SELECT @row:=-1) x

这将用于为每个条目生成与日期间隔中的天数一样多的行。

SELECT DATEDIFF(`end`,`begin`) as number_of_days FROM `opening_periods`

所有放在一起看起来像这样:

SELECT DATE_ADD( o.`begin`, INTERVAL days day) as date_field, t.begin, t.end
FROM `opening_periods` o INNER JOIN (
SELECT id, rown as days
FROM `opening_periods`, 
(SELECT @row := @row + 1 as rown FROM 
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t1,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, 
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3, 
(SELECT @row:=-1) x) numbers_table
WHERE rown <= DATEDIFF(`end`,`begin`)) r
ON o.id = r.id
INNER JOIN `opening_times` t ON o.`opening_time_id` = t.id
ORDER BY o.id

这是一个小提琴:http://rextester.com/AKDRI84101

【讨论】:

  • 哇,这是一个非常密集的查询 moni_dragu,感谢您的努力!我会检查一下,因为有些东西对我来说真的很新(例如@row:以前从未见过)。困难在于将我的另外两张表添加到该查询中:一张带有演出时段,一张带有活动时段。
  • @row 是用户定义的变量。您可以在文档中阅读更多内容:dev.mysql.com/doc/refman/5.7/en/user-variables.html 或在 SO:stackoverflow.com/a/1010042/2022457
猜你喜欢
  • 2014-06-11
  • 2013-08-09
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
相关资源
最近更新 更多