如何在mysql数据库中存储日期? [英] how store date in mysql database?

查看:74
本文介绍了如何在mysql数据库中存储日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期为dd/mm/yyyy格式.如果以后我想对它们进行一些操作,如何将其存储在数据库中?

i have date in dd/mm/yyyy format. how can i store it in databse, if i fant to do some operations on them after?

例如,我必须找出date > something

我必须设置为date field的哪种类型?

what type i must set to date field?

谢谢

推荐答案

要在MySQL中存储日期或时间,请使用

To store dates or times in MySQL use date, datetime or timestamp. I'd recommend the first two for most purposes.

要告诉MySQL如何解析日期格式,请使用

To tell MySQL how to parse your date format use the STR_TO_DATE function. Here's an example:

CREATE TABLE table1 (`Date` Date);
INSERT INTO table1 (`Date`) VALUES (STR_TO_DATE('01/05/2010', '%m/%d/%Y'));
SELECT * FROM table1;

Date
2010-01-05

要将结果格式化为原始格式,请参见 DATE_FORMAT 函数.请注意,仅当您要使用默认格式以外的其他方式将其显示为字符串时,才需要对其进行格式化.

To format the results back into the original form look at the DATE_FORMAT function. Note that you only need to format it if you want to display it as a string using something other than the default format.

这篇关于如何在mysql数据库中存储日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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