简体   繁体   English

基于星期几的不同选择查询? -MySQL

[英]Different select query based on the day of week? - MySQL

Is it possible to execute a different select query for each day of the week. 是否可以在一周的每一天执行不同的选择查询。 I currently have the following columns: id, station_name, week_type and service. 我目前有以下几列:id,station_name,week_type和service。

The week_type is an enom value with the following options: 'Mon-Thur', 'Fri', 'Sat', 'Sun', 'Special'. week_type是一个enom值,具有以下选项:'Mon-Thur','Fri','Sat','Sun','Special'。

The service column only has a varchar value of the time of day. 服务列仅具有一天中的时间的varchar值。 It needs to apply as the service operates the same on a weekly schedule depending on the week_type. 它需要应用,因为该服务根据week_type在每周的时间表上运行相同。

+-----------------------------------+------------+-----------+-----------+
|               id                  |station_name| week_type |  service  |
+-----------------------------------+------------+-----------+-----------+
|               1                   | Station1   |  Mon-Thur |  08:15:00 |
|                                   |            |           |           |
|               2                   | Station2   |    Sat    |  10:15:00 |
+-----------------------------------+------------+-----------+-----------+

As seen in the table above, when it is Saturday in my timezone and is equal to the week_type, then it should only show Saturday rows. 如上表所示,当我所在时区中的星期六是星期几,并且等于week_type时,它应该只显示星期六的行。 And etc. for the other columns. 其他列等等。

Any help would be much appreciated, as I am new to SQL. 任何帮助将不胜感激,因为我是SQL新手。

I think you really need to work out on the table. 我认为您确实需要在桌子上锻炼。 Why don't you normalize your table. 你为什么不规范你的表。

station_services

id|station_name

station_working_days

id|station_id|weekday_id|working_hours

If you dont want week days as seperate table then you can hardcode from 1 as sunday to saturday as 7 如果您不希望将工作日作为单独的表格,则可以将1硬编码为星期日,而将星期六硬编码为7

station_working_days

id|station_id|weekday|working_hours

By normalising you will get all the flexibility in future too. 通过规范化,您将来也将获得所有的灵活性。

In case if the stations all the time have the same working hours then use the following table normalisation so that it may help you. 如果各站始终都具有相同的工作时间,请使用下表标准化来为您提供帮助。

station_services

id|station_name|working_hours


station_working_days

id|station_id|weekday_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM