mysql - 存储一系列值 [英] mysql - storing a range of values

查看:70
本文介绍了mysql - 存储一系列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资源的可用性字段,其中列出了一天中哪些小时可供使用?

I have a resource that has a availability field that lists what hours of a day its available for use?

例如.res1 在一天的 0-8,19-23 小时之间可用,这里的范围可以是小时范围的逗号分隔值.例如 0-23 表示 24 小时访问,0-5,19-23 或 0-5,12-15,19-23

eg. res1 available between 0-8,19-23 hours on a day, the range here can be comma separated values of hour ranges. e.g are 0-23 for 24 hour access, 0-5,19-23 or 0-5,12-15,19-23

存储这个的最佳方式是什么?char 是一个不错的选择吗?当资源被访问时,我的php需要用这里定义的小时检查当前小时,然后决定是否允许此访问.我可以让mysql告诉我当前的小时是否在此处指定的范围内?

What's the best way to store this one? Is char a good option? When the resource is being accessed, my php needs to check the current hour with the hour defined here and then decide whether to allow this access or not. Can I ask mysql to tell me if the current hour is in the range specified here?

推荐答案

我将项目可用性存储在一个单独的表中,其中每一行我都有(以您的示例为例):

I'd store item availability in a separate table, where for each row I'd have (given your example):

id、startHour、endHour、resourceId

id, startHour, endHour, resourceId

而且我只使用整数作为开始时间和结束时间.然后,您可以使用 HOUR(NOW()) 或您拥有的功能对连接进行查询,以查看一天中特定时间的可用性.

And I'd just use integers for the start and end times. You can then do queries against a join to see availability given a certain hour of the day using HOUR(NOW()) or what have you.

(另一方面,对于此类数据,我更喜欢使用像 MongoDb 这样的非关系型数据库)

(On the other hand, I would've preferred a non-relational database like MongoDb for this kind of data)

这篇关于mysql - 存储一系列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆