MySQL - 组合两个字段来创建一个unix时间戳?

时间:2010-04-21 14:55:57

标签: mysql unix timestamp

我正在尝试通过在表中组合日期和时间字段来从查询中检索UNIX时间戳,但它会一直返回为零。

            SELECT *, 
            UNIX_TIMESTAMP(startdate starttime) AS start,  
            UNIX_TIMESTAMP(enddate endtime) AS end
            FROM mytable; 

任何人都可以帮助我吗?

感谢。

2 个答案:

答案 0 :(得分:4)

未经测试但应该有效:

SELECT UNIX_TIMESTAMP(CONCAT(startdate, ' ', starttime)) AS start;

答案 1 :(得分:1)

看看这个:

mysql> select TIMESTAMP(curdate(), curtime()), TIMESTAMP(curdate(), curtime()) + 0;
+---------------------------------+-------------------------------------+
| TIMESTAMP(curdate(), curtime()) | TIMESTAMP(curdate(), curtime()) + 0 |
+---------------------------------+-------------------------------------+
| 2010-04-21 19:03:23             |                      20100421190323 |
+---------------------------------+-------------------------------------+
1 row in set (0.00 sec)

对不起,还有一些补充:

mysql> select UNIX_TIMESTAMP(TIMESTAMP(curdate(), curtime())), UNIX_TIMESTAMP();
+-------------------------------------------------+------------------+
| UNIX_TIMESTAMP(TIMESTAMP(curdate(), curtime())) | UNIX_TIMESTAMP() |
+-------------------------------------------------+------------------+
|                                      1271862564 |       1271862564 |
+-------------------------------------------------+------------------+
1 row in set (0.00 sec)