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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看