如何更改MySQL数据库的日期格式?
我们将 MySQL 数据库与 FileMaker 结合使用。看起来,当 FileMaker 读取 MySQL 表时,它只接受 m/d/y 格式的日期。
有什么方法可以让我们的 MySQL 数据库将其默认格式更改为 m/d/y 而不是 YYYY-MM-DD 吗?
我知道我可以在单个 SELECT 查询上使用 DATE_FORMAT() 函数,但我想看看是否可以更改默认格式。
We are using a MySQL database with FileMaker. It appears that when FileMaker is reading the MySQL tables, it will only accept dates in the format of m/d/y.
Is there any way I can get our MySQL database to change its default format to be m/d/y instead of YYYY-MM-DD?
I know I can use the DATE_FORMAT() function on individual SELECT queries but I want to see if I can just change the default formatting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我使用 WAMP 安装,通常只需创建一个列
INT(10)
,然后像这样存储我的日期:这将
2013-11-22 12:45:09
存储为像1322111245
这样的数字。是的,这可能被认为是“不恰当的”,但我不在乎。它有效,我可以在客户端上以任何我喜欢的方式轻松排序和格式化。如果您希望运行任何其他日期函数,显然不建议这样做,但对我来说,我通常只想知道记录的最后更新并按日期对结果集进行排序。
I use a WAMP installation and usually simply create a column
INT(10)
and then store my dates like this:This stores
2013-11-22 12:45:09
as a number like1322111245
. Yes, it may be considered "improper" but I don't care. It works, and I can sort easily and format on the client any which way I like.This is obviously not suggested if you expect to run any other date functions, but for me, I usually just want to know the record's last update and sort a result set by date.
阅读更多内容,我发现您可以更改特定的格式字段但不推荐。
您无法更改存储格式
您可以设置< a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_allow_invalid_dates" rel="nofollow noreferrer">ALLOW_INVALID_DATES 并使用你想要的格式,但我真的不推荐这样做。
如果您的字段未建立索引,则在进行选择时调用
DATE_FORMAT()
不会出现问题,唯一的问题是您是否需要对该字段进行选择,在这种情况下索引不会因为您正在调用该函数而被使用。Reading a little more I found you can change the format for an specific field but there is not recommended.
you cannot change the storage format
you could set ALLOW_INVALID_DATES and save the dates with the format you wish but I really don't recommend that.
if your field isn't indexed there is not issue on call
DATE_FORMAT()
when you are doing the select, the only issue is if you need to make a select for that field in which case the index wont be used because you are calling the function.诀窍不是改变 MySQL 中的格式(无论如何它都不会存储为字符串),而是在查询时指定所需的格式。
这可以使用这样的方法来实现:
官方的 MySQL 手册在这里:http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
顺便说一下,题外话,但我觉得我应该提一下:我总是建议不要使用 mm/dd/yyyy 作为日期格式——你会让美国以外的任何人感到困惑;世界上几乎所有其他国家通常都会使用 dd/mm/yyyy。但这两种格式都是不明确的 - 当您说“12/05/2010”时,您指的是哪个日期?可能和我的不同,但实际上不可能知道你打算走哪条路。
如果您打算将日期用于显示目的,我总是将月份显示为单词或缩写,因为这样可以消除任何歧义。出于输入目的,请使用日历控件以避免任何可能的混淆。
The trick is not to change the format in MySQL (it isn't stored as a string anyway), but to specify the format you want when you query it.
This can be achieved using something like this:
The official MySQL manual for this is here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
By the way, off-topic but I feel I should mention it: I'd always recommend against using mm/dd/yyyy as a date format -- you'll confuse anyone from outside the US; virtually every other country in the world would normally use dd/mm/yyyy. But both those formats are ambiguous - what date do you mean when you say "12/05/2010"? probably a different one from me, but it's impossible to actually know which way round you intended.
If you're intending to use the a date for display purposes, I'd always show the month as a word or abbreviation, as this removes any ambiguity. For input purposes, use a calendar control to avoid any possible confusion.
我认为你不能,请参阅更改MySQL的日期格式。
有一些名为
date_format
和datetime_format
的系统变量看起来很有前途,但如果您尝试设置它们,则会收到错误,并且 文档说它们“未使用”。I don't think you can, see Changing MySQL's Date Format.
There are system variables called
date_format
anddatetime_format
that look promising, but if you try to set them, you'll get an error, and the documentation say they are 'unused'.