dswsl2016 2018-02-10 16:58
浏览 75
已采纳

在MySql中根据工作日和特定时间选择记录

I need to filter out specific events based on specific criteria, e.g.

  • [x] Mo
    • [x] 12:00
    • [  ] 14:00
  • [x] Tue
    • [  ] 12:00
    • [x] 15:30
  • [x] Fri
    • [  ] 10:00
    • [  ] 11:30
    • [x] 17:15
    • [x] 18:00

which gives the following array in php:

array(
   0 => [[12, 00]],
   1 => [[15, 30]],
   4 => [[17, 15], [18, 00]]
)

The final array may be adjusted to the needs, however now I want to select from schedule_event table which has starts_at property that stores datetime.

Question is, is it possible to select specific events, so e.g. Monday will return those starting on 12:00 (Tuesday shouldn't), Friday will return 17:15 and 18:00.

So far I had this:

... WEEKDAY(se.startsAt) IN (:weekDays) ...

And :weekDays was simply array_keys of the given array. Now, I don't know how to actually include times now.

  • 写回答

2条回答 默认 最新

  • doujia1904 2018-02-10 17:20
    关注

    You have to create pairs of weekday - hour [- minute] conditions to achieve the expected outcome:

    select ...
    from ...
    where WEEKDAY(se.startsAt)=0 and hour(se.startsAt)=12
       or WEEKDAY(se.startsAt)=1 and hour(se.startsAt)=15 and minute(se.startsAt)=30
       ...
    

    Instead of using hour and minute functiond, you can also use a single date_format() call, but I'll leave that to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测