MySQL 存储生日的最佳字段是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10389560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:11:47  来源:igfitidea点击:

What is the best field to store the birthday?

mysql

提问by xRobot

I have to store in my mysql database, the users's birthday ( like 12-11-1990).

我必须在我的 mysql 数据库中存储用户的生日(如12-11-1990)。

What field type do I have to use to store it ?

我必须使用什么字段类型来存储它?

回答by oezi

to store a date, you should probably use a date. to quote the documentation:

要存储日期,您可能应该使用 date。引用文档:

The DATE type is used for values with a date part but no time part.

DATE 类型用于具有日期部分但没有时间部分的值。

回答by Tiago

You can store it as a DATEobject as you would normally do with non-repeating dates. The, if you're using MySQL (I believe other DBMS also have functions for this) you can retrieve birthdays using MySQL functions as described here:

您可以将其存储为DATE对象,就像通常使用非重复日期一样。的,如果你使用MySQL(我相信其他DBMS也有此功能)使用MySQL的功能描述,你可以检索生日在这里

SELECT * 
FROM table_name 
WHERE 
    MONTH(date_field) = desired_month AND 
    DAY(date_field) = desired_day

This method can be a bit slow when dealing with thousands of records. If that's your case, you can store the birthday with 3 separate INTs, one for each (year, month, day). Then you search birthdays like this:

这种方法在处理数千条记录时可能会有点慢。如果是这种情况,您可以使用 3 个单独的 INT 存储生日,每个(年、月、日)一个。然后你像这样搜索生日:

SELECT *
FROM table_name
WHERE
   month_field = desired_month AND
   day_field = desired_day

If you use this last method, I'd advise you to also store a DATETIME version of the birthday so that you can build the date without any kind of maneuvers. You can additionally index each one of the 3 fields (non-unique) so it's faster to retrieve them.

如果您使用最后一种方法,我建议您还存储生日的 DATETIME 版本,以便您无需任何操作即可构建日期。您还可以为 3 个字段(非唯一)中的每一个添加索引,以便更快地检索它们。

回答by Beenish Khan

"DATE" should be good.

“日期”应该不错。

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

DATE 类型用于具有日期部分但没有时间部分的值。MySQL 以 'YYYY-MM-DD' 格式检索和显示 DATE 值。支持的范围是“1000-01-01”到“9999-12-31”。

More details : http://dev.mysql.com/doc/refman/5.5/en/datetime.html

更多细节:http: //dev.mysql.com/doc/refman/5.5/en/datetime.html

回答by Adam Gent

You should store the date as DATE.

您应该将日期存储为 DATE。

And the Localeas a varchar (PHPor Java).

并且 Locale作为 varchar (PHPJava)。

Don't forget the Locale :)

不要忘记语言环境:)

Nothing like getting an Email from something like Weibo (Chinese Twitter) when its not your birthday yet.

没有什么比在你生日还没有时从微博(china推特)之类的东西收到电子邮件更合适的了。

回答by Jason

is DATEtoo obvious?

DATE太明显了?