Mysql:设置DATETIME的格式为“DD-MM-YYYY HH:MM:SS”创建表时

发布于 2024-12-19 02:44:08 字数 229 浏览 1 评论 0 原文

经过谷歌搜索后,我找不到一种方法来创建一个带有 DATETIME 列的新表,默认格式设置为“DD-MM-YYYY HH:MM:SS

我看到一个教程,其中是在 phpmyadmin 中完成的,所以我怀疑我可以通过命令行使用 mysql,并在创建新表时实现同样的效果,

CREATE TABLE ()

提前谢谢

After googling around, I cannot find a way to create a new table with a DATETIME column with the default format set to 'DD-MM-YYYY HH:MM:SS'

I saw a tutorial in which it was done in phpmyadmin so I suspect that I could use mysql via command line and achieve the same thing when creating my new table with

CREATE TABLE ()

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

娇纵 2024-12-26 02:44:09

试试这个:

DATE NOT NULL FORMAT 'YYYY-MM-DD'

try this:

DATE NOT NULL FORMAT 'YYYY-MM-DD'
高跟鞋的旋律 2024-12-26 02:44:08

“MySQL 以‘YYYY-MM-DD HH:MM:SS’格式检索并显示 DATETIME 值。”
这是来自 mysql 网站。您只能存储此类型,但当您需要显示它时,可以使用多种时间格式函数之一来更改它。

Mysql 时间和日期函数

例如,其中一个函数是 DATE_FORMAT,可以像这样使用:

SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM tablename

"MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format."
This is from mysql site. You can store only this type, but you can use one of the many time format functions to change it, when you need to display it.

Mysql Time and Date functions

For example, one of those functions is the DATE_FORMAT, which can be used like so:

SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM tablename
来日方长 2024-12-26 02:44:08

使用 DATE_FORMAT 函数更改格式。

SELECT DATE_FORMAT(CURDATE(), '%d/%m/%Y')

SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename

请参阅 DOC 了解更多详细信息

Use DATE_FORMAT function to change the format.

SELECT DATE_FORMAT(CURDATE(), '%d/%m/%Y')

SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename

Refer DOC for more details

滥情空心 2024-12-26 02:44:08

正如其他人所解释的那样,这是不可能的,但这是替代解决方案,它需要一些调整,但它的工作原理类似于日期时间列。

我开始思考,如何才能使格式化成为可能。我有一个主意。为它制作触发器怎么样?我的意思是,添加类型为 char 的列,然后使用 MySQL 触发器更新该列。这奏效了!我做了一些与触发器相关的研究,最后提出了这些查询:

CREATE TRIGGER timestampper BEFORE INSERT ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
CREATE TRIGGER timestampper BEFORE UPDATE ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');

你不能使用TIMESTAMPDATETIME作为列类型,因为它们有自己的格式,并且它们会自动更新。

所以,这是您的替代时间戳或日期时间替代方案!希望这有帮助,至少我很高兴我能做到这一点。

As others have explained that it is not possible, but here's alternative solution, it requires a little tuning, but it works like datetime column.

I started to think, how I could make formatting possible. I got an idea. What about making trigger for it? I mean, adding column with type char, and then updating that column using a MySQL trigger. And that worked! I made some research related to triggers, and finally come up with these queries:

CREATE TRIGGER timestampper BEFORE INSERT ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
CREATE TRIGGER timestampper BEFORE UPDATE ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');

You can't use TIMESTAMP or DATETIME as a column type, because these have their own format, and they update automatically.

So, here's your alternative timestamp or datetime alternative! Hope this helped, at least I'm glad that I got this working.

罗罗贝儿 2024-12-26 02:44:08

我使用了以下代码行&它工作得很好,谢谢......@Mithun Sasidharan
**

从表名中选择 DATE_FORMAT(column_name, '%d/%m/%Y')

**

i have used following line of code & it works fine Thanks.... @Mithun Sasidharan
**

SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename

**

抽个烟儿 2024-12-26 02:44:08

我很确定你不能更改 mysql 中的日期时间格式。 phpmyadmin 设置可能在读取日期时间时应用自定义格式(使用 DATE_FORMAT 或 php 中的某些内容)。数据库使用什么格式并不重要,应用程序中的格式可以按照您的意愿显示它。

日期格式化是一项非常常见的任务。我通常喜欢将其抽象为国际化代码,或者,如果您不需要处理 i18n,则将其抽象为通用日期实用程序库。它有助于保持事物的一致性并使以后更容易更改(或添加 i18n 支持)。

I'm pretty certain that you can't change the datetime format in mysql. The phpmyadmin setting is probably applying a custom format as it reads the datetime (using DATE_FORMAT or something from php). It shouldn't matter what format the database uses, format in the application to display it as you wish.

Date formatting is a pretty common task. I typically like to abstract it out into internationalization code or, if you don't need to deal with i18n, into a common date utility library. It helps keep things consistent and makes it easier to change later (or add i18n support).

北风几吹夏 2024-12-26 02:44:08

不,你不能;仅在创建表时,日期时间才会以默认格式存储,然后您可以使用 select 查询中的显示格式/doc/refman/5.5/en/date-and-time-functions.html" rel="nofollow">Mysql 日期时间函数

No you can't; datetime will be stored in default format only while creating table and then you can change the display format in you select query the way you want using the Mysql Date Time Functions

活泼老夫 2024-12-26 02:44:08

不能对表执行此操作;此外,您甚至根本无法更改此默认值。

答案是服务器变量 datetime_format< /strong>,未使用。

This cannot be done for the table; besides, you even cannot change this default value at all.

The answer is a server variable datetime_format, it is unused.

执笏见 2024-12-26 02:44:08
Dim x as date 

x = dr("appdate")
appdate = x.tostring("dd/MM/yyyy")

dr 是 datareader 的变量

Dim x as date 

x = dr("appdate")
appdate = x.tostring("dd/MM/yyyy")

dr is the variable of datareader

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