mysql timespan_Timespan-在mysql中检查工作日和一天中的时间

bd96500e110b49cbb3cd949968f18be7.png

I need to have a database with different rates at different time of day and on different weekdays.

For example:

Between 10:00 and 16:00 monday to friday I have one rate.

Between 16:00 and 10:00 monday to friday I have another. And on the weekends there is another rate.

The problem is when the time goes over midnight.

I have looked around for a solution. I have found this thread that I thought would work for me but it doesn't. At least I can't get it to work as I want it to.

Dealing with times and after midnight

The other solution I tried didn't work either. It was to store time as TIME in mysql and check if the time and day was between the timespan in the database. This didn't work out either because I didn't find a good solution for when the timespan goes over midnight.

How should I try to solve this? What should the database look like and the query to get the data?

Edit:

Here is what it got so far. starttid = start time, sluttid = end time. Ovrig_tid is used when it's not any of the specific times. startdag = start day of week, slutdag = end day of week. 0 = monday, 6 = sunday. So the result should be max one from every bolag_id.

解决方案

EDIT - Modified to meet criteria (end time) specified in comments:

I believe what you'll want to do is store each day's rate separately. Store at least one value with the last minute of a given day as a final catch-all rate (this will be the only row for days with a single rate all day). At any given day/time, just consult this table to determine the given rate for that period of time. See below:

DROP TABLE IF EXISTS tRate;

CREATE TABLE tRate (

rateId INT(11) UNSIGNED NOT NULL auto_increment,

rateDay TINYINT(1),

rateEndTime TIME,

rate DECIMAL(9,2),

PRIMARY KEY (rateId)

)

;

INSERT INTO tRate VALUES

(NULL, 0, '00:10:00', '0.80'),

(NULL, 0, '23:59:59', '0.90'),

(NULL, 1, '00:10:00', '0.90'),

(NULL, 1, '00:16:00', '0.75'),

(NULL, 1, '23:59:59', '0.90')

-- (etc. for all days 0-6)

;

SET @execDay = DATE_FORMAT(NOW(), '%w'); -- 1 in the case of today for the resultset below

SET @execTime = DATE_FORMAT(NOW(), '%H:%m:%s'); -- 14:02:33 at the time this example was run

Given this data, the following query:

SELECT *

FROM

tRate

WHERE

rateDay = @execDay

and @execTime < rateEndTime

;

Returns the resultset for the particular execution time:

+--------+---------+-------------+------+

| rateId | rateDay | rateEndTime | rate |

+--------+---------+-------------+------+

| 5 | 1 | 23:59:59 | 0.90 |

+--------+---------+-------------+------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值