如何更改MySQL数据库的日期格式?

发布于 2024-09-29 23:27:24 字数 210 浏览 6 评论 0原文

我们将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

傻比既视感 2024-10-06 23:27:25

我使用 WAMP 安装,通常只需创建一个列 INT(10),然后像这样存储我的日期:

UPDATE  `test` SET  `dateandtime` = DATE_FORMAT( NOW(),  '%y%m%d%H%i' ) WHERE `id` =1234;

这将 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:

UPDATE  `test` SET  `dateandtime` = DATE_FORMAT( NOW(),  '%y%m%d%H%i' ) WHERE `id` =1234;

This stores 2013-11-22 12:45:09 as a number like 1322111245. 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.

云裳 2024-10-06 23:27:25
SELECT COUNT(field_name) AS count_it FROM table_name WHERE DATE_FORMAT(date_field,'%Y-%m-%d') = DATE_FORMAT(CURDATE(), '%Y-%m-%d')-- to count records for today.
SELECT COUNT(field_name) AS count_it FROM table_name WHERE DATE_FORMAT(date_field,'%Y-%m-%d') = DATE_FORMAT(CURDATE(), '%Y-%m-%d')-- to count records for today.
百善笑为先 2024-10-06 23:27:25
SELECT DATE_FORMAT(NAME_COLUMN, "%d/%l/%Y %H:%i:%s") AS 'NAME'
SELECT DATE_FORMAT(NAME_COLUMN, "%d/%l/%Y %H:%i:%s") AS 'NAME'
浪漫人生路 2024-10-06 23:27:24

阅读更多内容,我发现您可以更改特定的格式字段但不推荐。

您无法更改存储格式

您可以设置< 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.

爱她像谁 2024-10-06 23:27:24

诀窍不是改变 MySQL 中的格式(无论如何它都不会存储为字符串),而是在查询时指定所需的格式。

这可以使用这样的方法来实现:

SELECT date_format(mydatefield,'%m/%d/%Y') as mydatefield FROM mytable

官方的 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:

SELECT date_format(mydatefield,'%m/%d/%Y') as mydatefield FROM mytable

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.

幸福丶如此 2024-10-06 23:27:24

我认为你不能,请参阅更改MySQL的日期格式

有一些名为 date_formatdatetime_format 的系统变量看起来很有前途,但如果您尝试设置它们,则会收到错误,并且 文档说它们“未使用”。

I don't think you can, see Changing MySQL's Date Format.

There are system variables called date_format and datetime_format that look promising, but if you try to set them, you'll get an error, and the documentation say they are 'unused'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文