MySQL:将 INT 转换为 DATETIME

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2367603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:26:22  来源:igfitidea点击:

MySQL: Convert INT to DATETIME

mysqldatetime

提问by pix0r

I have a UNIX-type timestamp stored in an INTcolumn in MySQL. What is the proper way to retrieve this as a MySQL DATETIME?

INT在 MySQL 的一列中存储了一个 UNIX 类型的时间戳。将其作为 MySQL 检索的正确方法是什么DATETIME

(I found the answer when re-scanning the MySQL Date functions, but didn't see the answer on SO. Figured it should be here.)

(我在重新扫描 MySQL 日期函数时找到了答案,但在 SO 上没有看到答案。认为它应该在这里。)

回答by user2970312

select from_unixtime(column,'%Y-%m-%d') from myTable; 

回答by Quassnoi

SELECT  FROM_UNIXTIME(mycolumn)
FROM    mytable

回答by yoben

The function STR_TO_DATE(COLUMN, '%input_format')can do it, you only have to specify the input format. Example : to convert p052011

函数STR_TO_DATE(COLUMN, '%input_format')可以做到,你只需要指定输入格式。示例:转换 p052011

SELECT STR_TO_DATE('p052011','p%m%Y') FROM your_table;

The result : 2011-05-00

结果:2011-05-00