我应该使用 DATE 和 TIME 作为字段而不是 DATETIME
我有一个关于电视指南的项目,在数据库中名为 mytvguide - 我使用 PHPMyAdmin。这是一个表的结构,称为 tvshow1:
Field Type
channel varchar(255)
date date No
airdate time No
expiration time No
episode varchar(255)
setreminder varchar(255)
但我不确定如何获取 DATE、TIME 来使用分页脚本(下面是脚本,适用于带有 DATETIME 的版本): http://pastebin.com/6S1ejAFJ
然而,尽管 DATETIME 有效 - 它显示当天播出的节目,如下所示:
Programme 1 showing on Channel 1 2:35pm "Episode 2" Set Reminder
Programme 1 showing on Channel 1 May 26th - 12:50pm "Episode 3" Set Reminder
Programme 1 showing on Channel 1 May 26th - 5:55pm "Episode 3" Set Reminder
但是我不太确定如何为使用上面所示的日期、时间函数的字段复制它。
I have a project on going for a TV guide, called mytvguide in the database - I use PHPMyAdmin. This is the structure for one table, which is called tvshow1:
Field Type
channel varchar(255)
date date No
airdate time No
expiration time No
episode varchar(255)
setreminder varchar(255)
but am not sure how to get DATE, TIME to work with the pagination script (below is the script, which works for the version with DATETIME): http://pastebin.com/6S1ejAFJ
However, although the DATETIME one works - it shows programmes that air on the day itself like this:
Programme 1 showing on Channel 1 2:35pm "Episode 2" Set Reminder
Programme 1 showing on Channel 1 May 26th - 12:50pm "Episode 3" Set Reminder
Programme 1 showing on Channel 1 May 26th - 5:55pm "Episode 3" Set Reminder
but I'm not quite sure how to replicate that for the fields that use DATE, TIME functions as seen above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答您的问题标题:
datetime
类型非常方便,您始终可以使用 date() 或 time() 函数格式化此字段以获取适当的部分,或来自 的任何其他函数巨大列表至于问题正文,字段类型与分页无关。
有一个特别的问题吗?
Answering to your question title:
datetime
type is quite handy, and you always can format this field using date() or time() functions to get the appropriate part, or any other function from the huge listAs for the question body, field type has nothing to do with pagination.
Got a particular question?
在 MySQL 中,您可以使用
DATE_FORMAT
(请参阅 manual)例如:
否则你只需按原样提取值并使用 PHP 提供的时间/日期函数修改它
In MySQL you can use
DATE_FORMAT
(see manual)For instance:
Otherwise you just pull the value as it is and modify it with the time/date function that PHP provides
如果您希望数据库能够扩展,则应该遵循一条重要规则:避免使用每行函数,例如
if
、coalescse
、case
和是的,date()
和time()
。表中的属性(列)应该是您处理的最小单位。如果您需要按日期搜索,请单独存储日期。 不要将其组合到日期时间中,您必须在
select
语句中提取它。将属性组合到单个字段中的人,例如将日期组合成日期时间,或者,恐怖中的恐怖,将逗号分隔的列表,您想要检查其中的各个部分,正在给自己和他们的继任者带来巨大的伤害。
通常最好将两个字段粘贴在一起(例如将日期和时间粘贴到分页脚本的日期时间中),而不是尝试以其他方式拆分(用于其他目的)。
当然,如果您从不打算在查询中单独使用日期和时间,或者您希望数据库保持较小,请随意忽略我的咆哮:-)
If you ever want your database to scale, you should follow one important rule: Avoid per-row functions, like
if
,coalescse
,case
and yes,date()
andtime()
.An attribute (column) in a table should be the smallest unit you handle. If you will need to search on date, store the date separately. Don't combine it into a datetime where you will have to extract it in your
select
statements.People who combine attributes into a single fields such as date into a datetime or, horror of horrors, a comma-separated-list which you want to check individual parts of, are doing themselves and their successors a huge disservice.
You're usually better off pasting two fields together (such as date and time into a datetime for your pagination script) than trying to split the other way (for other purposes).
Of course, if you never intend to use the date and time separately in your queries, or if you expect your database to remain small, feel free to ignore my rant :-)