如何在MySQL中将DATETIME转换为TIMESTAMP? [英] How to convert DATETIME to TIMESTAMP in mysql?

查看:1446
本文介绍了如何在MySQL中将DATETIME转换为TIMESTAMP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表中有DATETIME列,具有2015-04-23 11:17:49属性 尝试将其转换为unix时间戳,根据mysql文档,我只需要将该字段放入UNIX_TIMESTAMP()函数中,我将获得-> 1223423442-时间戳,但是它不起作用,我只有0000-00- 00 00:00:00 尝试了很多东西:

I have DATETIME column in my table, with 2015-04-23 11:17:49 properties Trying to convert it to unix timestamp, acording to the mysql documentation I need just put the field into UNIX_TIMESTAMP() function and I'll get -> 1223423442 - timestamp but it's doesn't work, I've got only 0000-00-00 00:00:00 Tried a lot of stuff:

// doesn't work
UNIX_TIMESTAMP(CAST(`updated` AS CHAR(100))) AS updated_at,
// doesn't work
UNIX_TIMESTAMP(`updated`) AS updated_at,
//doesn't work
UNIX_TIMESTAMP(STR_TO_DATE(CAST(`created` AS CHAR(100)), \'%M %e %Y %h:%i%p\'))
                                AS created_at'
// doesn't work
UNIX_TIMESTAMP(STR_TO_DATE(`created`, '%M %e %Y %h:%i%p'))
                                AS created_at

没有``不能正常工作,我错过了什么吗?

Without `` doesn't work as well, am I missing something?

推荐答案

尝试:

select 
  o1.id, 
  o1.operation_date_time, 
  (unix_timestamp(o2.operation_date_time) - unix_timestamp(o1.operation_date_time)) 
    as duration 
from operations as o1 
  inner join operations as o2
where o1.operation = "START" 
  and o2.operation = "STOP"
  and o1.id = (o2.id - 1);

它应该给出作为输出:

+------+---------------------+----------+
| id   | operation_date_time | duration |
+------+---------------------+----------+
|    1 | 2000-01-01 06:30:45 |     4455 |
|    3 | 2000-01-01 08:18:12 |    11146 |
|    5 | 2000-01-01 15:45:01 |    11792 |
+------+---------------------+----------+
3 rows in set (0.00 sec)

这篇关于如何在MySQL中将DATETIME转换为TIMESTAMP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆