存放生日礼物的最佳场地是什么?

时间:2022-10-22 18:13:43

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 ?

我需要使用什么字段类型来存储它?

5 个解决方案

#1


17  

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

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

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

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

#2


10  

You can store it as a DATE object 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:

您可以将它存储为一个日期对象,就像您通常对非重复日期所做的那样。如果你正在使用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个不同的INTs存储生日,每个INTs(年、月、日)一个INTs。然后你会这样搜索生日:

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.

如果您使用最后一种方法,我建议您也存储一个日期时间版本的生日,以便您可以在没有任何操作的情况下构建日期。您可以为这3个字段中的每个字段(非唯一字段)添加索引,以便更快地检索它们。

#3


3  

"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'.

日期类型用于具有日期部分但没有时间部分的值。MySQL检索并显示“YYYY-MM-DD”格式的日期值。支持的范围是“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。

#4


3  

You should store the date as DATE.

您应该将日期存储为日期。

And the Locale as a varchar (PHP or Java).

并且区域设置为varchar (PHP或Java)。

Don't forget the Locale :)

不要忘记语言环境:)

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

没有什么比收到微博(中国的推特)这样的邮件更让人兴奋的了。

#5


1  

is DATE too obvious?

日期太明显了吗?

#1


17  

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

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

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

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

#2


10  

You can store it as a DATE object 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:

您可以将它存储为一个日期对象,就像您通常对非重复日期所做的那样。如果你正在使用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个不同的INTs存储生日,每个INTs(年、月、日)一个INTs。然后你会这样搜索生日:

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.

如果您使用最后一种方法,我建议您也存储一个日期时间版本的生日,以便您可以在没有任何操作的情况下构建日期。您可以为这3个字段中的每个字段(非唯一字段)添加索引,以便更快地检索它们。

#3


3  

"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'.

日期类型用于具有日期部分但没有时间部分的值。MySQL检索并显示“YYYY-MM-DD”格式的日期值。支持的范围是“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。

#4


3  

You should store the date as DATE.

您应该将日期存储为日期。

And the Locale as a varchar (PHP or Java).

并且区域设置为varchar (PHP或Java)。

Don't forget the Locale :)

不要忘记语言环境:)

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

没有什么比收到微博(中国的推特)这样的邮件更让人兴奋的了。

#5


1  

is DATE too obvious?

日期太明显了吗?