我有一个像这样的表结构:
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运行,这将为该日期范围内每一天发生的事件返回一行。
因此,例如,如果用户在接下来的四周内每周一和周三都有一个事件,那么今天将有一行作为start_date,将4周时间作为end_date,其中MON和WED设置为' 1'
我需要的是一个查询,在这种情况下,如果我在该范围内使用三周搜索周期将返回6行,每行包含事件发生的日期。
我对如何处理此问题感到困惑,任何帮助表示赞赏!
答案 0 :(得分:-1)
在这里(假设日期范围不超过19年):
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
user_id = 30 (在SQL中输入8次):
day
==========
2018-03-02
2018-03-04
2018-03-09
2018-03-11
结果:
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
对于 user_id = 31 (它在SQL中输入了8次):
day
==========
2018-02-20
2018-02-21
2018-02-22
2018-02-23
结果:
SELECT *from posts
where REPLACE(REPLACE(REPLACE(CONCAT(`primary_phone`,`workphone`,`cellphone`),'-',''),')',''),'(','')
like '%xxxxxxxx%'
轻松自负。