简体   繁体   English

MySQL视图:将Unix时间戳转换为日期时间

[英]MySQL view: convert unix timestamp to datetime

How do I convert timestamps in a MySQL View? 如何在MySQL视图中转换时间戳?

I have a MySQL View for data exchange between 2 different software tools. 我有一个MySQL View,用于2种不同软件工具之间的数据交换。 The original table is a unix timestamp as int(11) . 原始表是unix时间戳,如int(11) The other software expects a DATETIME. 其他软件期望DATETIME。

I have a VIEW like this: 我有这样的观点:

CREATE VIEW `myView` AS 
select 
date_format(from_unixtime(`myUnixTimestamp`),_utf8'%d/%m/%Y %H:%i:%s') 
AS `Date-Entered`
from ...

This VIEW statement creates a varchar(24) - which is better than nothing as it can be converted - but the conversion makes some problems. 该VIEW语句创建了一个varchar(24)-可以转换它比没有要好-但是转换会带来一些问题。

Is there any way to create a DATETIME field instead of a VARCHAR with the view? 有什么方法可以用视图创建DATETIME字段而不是VARCHAR吗?

You can cast the value to datetime : 您可以将值转换为datetime

-- print a UNIX timestamp (as returned by NOW()) as DATETIME
SELECT CAST( DATE_FORMAT( NOW( ) , '%Y-%m-%d' ) AS DATETIME );

Thanks for the answers, I tried some combinations, and this one worked (so easy, its a shame...): 感谢您的回答,我尝试了一些组合,并且此组合有效(非常简单,很可惜...):

CREATE VIEW `myView` AS 
select 
from_unixtime(`myUnixTimestamp`)
AS `Date-Entered`
from ...

View Structure shows: Date-Entered, type: datetime 视图结构显示:输入日期,类型:datetime

try 尝试

CREATE VIEW `myView` AS 
select 
FROM_UNIXTIME([unix timestamp],'%d/%m/%Y %H:%i:%s') 
AS `Date-Entered`
from ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM