MySQL查询返回两个日期范围之间的行

西里尔·伯恩

我有一个这样的表结构:

USER_ID START_DATE END_DATE   MON TUE WED THU FRI SAT SUN  
======= ========== ========== === === === === === === ===
      1 2018-03-01 2018-03-15   0   1   0   1   0   0   0
      2 2018-02-20 2018-02-23   1   1   1   1   1   0   0

其中 M/T/W 等列是 1/0 以指示事件是否发生在星期一/星期二/星期三等。

所以我需要一个查询,我可以为两个日期之间的 USER_ID 运行该查询,该查询将为该日期范围内的每一天的每次事件返回一行。

因此,例如,如果用户在接下来的 4 周内每周一和周三都有一个事件,那么将有一行以今天作为开始日期,以 4 周时间作为结束日期,其中 MON 和 WED 设置为“1”。

我需要的是一个查询,在这种情况下,如果我在该范围内使用三周的搜索期,将返回 6 行,每行都包含事件发生的日期。

我对如何处理这个完全困惑,任何帮助表示赞赏!

穿刺者

给你(假设日期范围不超过 19 年):

create table schedule(
  user_id int primary key not null,
  start_date date not null,
  end_date date not null,
  mon int not null,
  tue int not null,
  wed int not null,
  thu int not null,
  fri int not null,
  sat int not null,
  sun int not null
);

insert into schedule (user_id, start_date, end_date, 
                      mon, tue, wed, thu, fri, sat, sun) 
  values (30, '2018-03-01', '2018-03-15', 0, 1, 0, 1, 0, 0, 0);

insert into schedule (user_id, start_date, end_date, 
                      mon, tue, wed, thu, fri, sat, sun) 
  values (31, '2018-02-20', '2018-02-23', 1, 1, 1, 1, 1, 0, 0);

对于user_id = 30(在 SQL 中输入了 8 次):

select date_add(start_date, interval 7 * (d2.n * 100 + d1.n * 10 + d0.n) + weekday.d day) as day
  from 
    (select 1 as n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0) d2,
    (select 1 as n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0) d1,
    (select 1 as n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0) d0,
    (select 0 as d from schedule where user_id = 30 and mon = 1
     union select 1 as d from schedule where user_id = 30 and tue = 1
     union select 2 as d from schedule where user_id = 30 and wed = 1
     union select 3 as d from schedule where user_id = 30 and thu = 1
     union select 4 as d from schedule where user_id = 30 and fri = 1
     union select 5 as d from schedule where user_id = 30 and sat = 1
     union select 6 as d from schedule where user_id = 30 and sun = 1) as weekday,
    schedule s
    where user_id = 30
      and date_add(start_date, interval 7 * (d2.n * 100 + d1.n * 10 + d0.n) + weekday.d day) between s.start_date and s.end_date
  order by d2.n, d1.n, d0.n

结果:

day
==========
2018-03-02
2018-03-04
2018-03-09
2018-03-11

对于user_id = 31(在 SQL 中输入了 8 次):

select date_add(start_date, interval 7 * (d2.n * 100 + d1.n * 10 + d0.n) + weekday.d day) as day
  from 
    (select 1 as n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0) d2,
    (select 1 as n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0) d1,
    (select 1 as n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0) d0,
    (select 0 as d from schedule where user_id = 31 and mon = 1
     union select 1 as d from schedule where user_id = 31 and tue = 1
     union select 2 as d from schedule where user_id = 31 and wed = 1
     union select 3 as d from schedule where user_id = 31 and thu = 1
     union select 4 as d from schedule where user_id = 31 and fri = 1
     union select 5 as d from schedule where user_id = 31 and sat = 1
     union select 6 as d from schedule where user_id = 31 and sun = 1) as weekday,
    schedule s
    where user_id = 31
      and date_add(start_date, interval 7 * (d2.n * 100 + d1.n * 10 + d0.n) + weekday.d day) between s.start_date and s.end_date
  order by d2.n, d1.n, d0.n

结果:

day
==========
2018-02-20
2018-02-21
2018-02-22
2018-02-23

十分简单。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

两个日期两次之间的MySQL查询

来自分类Dev

从MDX查询SSAS返回两个日期之间的数据

来自分类Dev

在MYSQL中获取两个日期范围之间的记录

来自分类Dev

MySQL查询-无需检索行即可在两个日期之间进行检查

来自分类Dev

MySQL返回两个日期之间的所有记录

来自分类Dev

MySQL查询使用时间戳数据在两个日期之间的两个时间范围之间进行搜索

来自分类Dev

SQL查询以获取两个日期之间月份的日期范围列表

来自分类Dev

SQL查询按日期范围查找介于两个日期之间的所有数据

来自分类Dev

两个日期之间的SQALCHEMY查询

来自分类Dev

返回两个日期之间的数据

来自分类Dev

返回两个日期之间的数据

来自分类Dev

结合两个mysql查询返回ok而不是行

来自分类Dev

mysql查询获取两个日期之间的第一个特定日期

来自分类Dev

如何使用WHERE子句查询两个日期范围之间的数据?-SQL

来自分类Dev

如何使用Cloud FireStore查询两个日期范围之间的数据?

来自分类Dev

php MySQL在两个日期时间之间查询,但如果在范围内完成,则包括范围外的分钟数

来自分类Dev

生成两个日期之间的日期范围

来自分类Dev

在两个日期之间过滤MYSQL

来自分类Dev

Mysql在两个日期之间删除

来自分类Dev

MYSQL在两个日期之间

来自分类Dev

选择两个日期之间的行

来自分类Dev

选择两个日期之间的行

来自分类Dev

MYSQL查询两个日期之间的数据,表没有日期列

来自分类Dev

mysql-如何获取两个时间范围之间的随机时间(而非日期)

来自分类Dev

过滤两个不同日期范围之间的pyspark数据框行

来自分类Dev

来自子查询的两个日期之间的SQL查询

来自分类Dev

MySQL查询以获取两个日期范围内的订单

来自分类Dev

PHP:两个日期之间的返回日期错误

来自分类Dev

两个日期之间的范围的NSPredicate无法正常工作

Related 相关文章

  1. 1

    两个日期两次之间的MySQL查询

  2. 2

    从MDX查询SSAS返回两个日期之间的数据

  3. 3

    在MYSQL中获取两个日期范围之间的记录

  4. 4

    MySQL查询-无需检索行即可在两个日期之间进行检查

  5. 5

    MySQL返回两个日期之间的所有记录

  6. 6

    MySQL查询使用时间戳数据在两个日期之间的两个时间范围之间进行搜索

  7. 7

    SQL查询以获取两个日期之间月份的日期范围列表

  8. 8

    SQL查询按日期范围查找介于两个日期之间的所有数据

  9. 9

    两个日期之间的SQALCHEMY查询

  10. 10

    返回两个日期之间的数据

  11. 11

    返回两个日期之间的数据

  12. 12

    结合两个mysql查询返回ok而不是行

  13. 13

    mysql查询获取两个日期之间的第一个特定日期

  14. 14

    如何使用WHERE子句查询两个日期范围之间的数据?-SQL

  15. 15

    如何使用Cloud FireStore查询两个日期范围之间的数据?

  16. 16

    php MySQL在两个日期时间之间查询,但如果在范围内完成,则包括范围外的分钟数

  17. 17

    生成两个日期之间的日期范围

  18. 18

    在两个日期之间过滤MYSQL

  19. 19

    Mysql在两个日期之间删除

  20. 20

    MYSQL在两个日期之间

  21. 21

    选择两个日期之间的行

  22. 22

    选择两个日期之间的行

  23. 23

    MYSQL查询两个日期之间的数据,表没有日期列

  24. 24

    mysql-如何获取两个时间范围之间的随机时间(而非日期)

  25. 25

    过滤两个不同日期范围之间的pyspark数据框行

  26. 26

    来自子查询的两个日期之间的SQL查询

  27. 27

    MySQL查询以获取两个日期范围内的订单

  28. 28

    PHP:两个日期之间的返回日期错误

  29. 29

    两个日期之间的范围的NSPredicate无法正常工作

热门标签

归档