简体   繁体   English

在MySQL中存储日期

[英]storing dates in mysql

Is it better to store dates in mysql in three columns or use just one column. 将日期存储在mysql中的三列中还是只使用一列是更好的选择。 Which one is faster. 哪一个更快。 Also, if I just want to do inserts with todays date in format dd/mm/yy , how do I do that. 另外,如果我只想以dd / mm / yy格式插入今天的日期,该怎么做。 and also how do I do selects with that. 以及我该如何选择。 Also, lets say if I wanted to get results for all the wednesdays, how do I do that or lets say one date 25th of all the months and years, how do i do that. 另外,如果我想获得所有星期三的结果,该怎么做?或者说在所有月份和年份的第25个日期,我该怎么做。

Thanks People. 谢谢大家。

I am using PHP with Apache and Mysql. 我在Apache和Mysql中使用PHP。

What are the drawbacks of using the structure that I am proposing. 使用我提出的结构有什么弊端。 I can easily get all the 25th by using the date table and I can get all the days using another column for days. 通过使用日期表,我可以很容易地获得所有25号数据,而在另一天中,我可以使用另一列获得所有的日期。 How much difference would be there in the terms of speed between my proposed solution and using a DATE table? 我提出的解决方案和使用DATE表之间在速度方面会有多少差异?

You will want to use a proper column type, such as DATE , DATETIME , or TIMESTAMP , depending on your needs. 您将根据需要使用适当的列类型,例如DATEDATETIMETIMESTAMP They are built specifically to handle dates, and can more easily perform other functions (adding, comparing, etc.) that would be difficult to perform on 3 separate columns. 它们专门为处理日期而构建,并且可以更轻松地执行其他功能(添加,比较等),而这些功能很难在3个单独的列上执行。

Read this for more info. 阅读以获得更多信息。

DAYOFWEEK(date) will give you a numeric representation for the day. DAYOFWEEK(date)将为您提供当天的数字表示。 In your case, 4 = Wednesday. 您的情况是4 =星期三。 DAYOFMONTH(date) will work for finding all 25th days of the month. DAYOFMONTH(date)将用于查找该月的所有第25天。

DAYNAME(date) will return the name of the weekday for date DAYNAME(date)将返回日期的工作日名称

Also, if I just want to do inserts with todays date in format dd/mm/yy ,how do I do that. 另外,如果我只想以dd / mm / yy格式插入今天的日期,该怎么做。

Well it depends on the format your date is passed in through your 
form but you are going to want to store your date in YYYY-mm-dd format.

INSERT INTO my_table (timefieldname) VALUES ( '$date' );

and also how do I do selects with that. 以及我该如何选择。

SELECT timefieldname FROM my_table;

//or you can format the date - this will give you month/day/year 01/01/2012
SELECT DATE_FORMAT(timefieldname, '%m/%d/%Y') FROM my_table;

Also, lets say if I wanted to get results for all the wednesdays, 另外,假设我想获得所有星期三的结果,

SELECT timefieldname FROM my_table WHERE DAYNAME(timefieldname) = 'Wednesday';

How do I do that or lets say one date 25th of all the months and years, how do i do that. 我该怎么做,或者说在所有月份和年份的25号,我该怎么做。

SELECT timefieldname FROM my_table WHERE DAY(timefieldname) = '25'; 

You can free up having to pass dates from your codebase and let mysql insert them for you, provided they are time stamps: 您可以释放从代码库传递日期的时间,并让mysql为您插入日期(如果它们是时间戳):

ALTER TABLE tablename ADD `timefieldname` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;

It's not much of a speed boost, but it does reduce your need to code and validate variables passed to the database. 速度的提高并不多,但是确实减少了对编码和验证传递给数据库的变量的需求。

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

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