将 MySQL 时间戳转换为 UNIX 时间戳?
若要将 MySQL 时间戳转换为 UNIX 时间戳,请使用 UNIX_TIMESTAMP()。以下是语法 −
select unix_timestamp(yourColumnName) from yourTableName;
让我们首先创建一个表 −
mysql> create table DemoTable( Duetimestamp timestamp ); Query OK, 0 rows affected (2.66 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(now()); Query OK, 1 row affected (1.53 sec) mysql> insert into DemoTable values('2016-01-21 12:34:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-05-01 02:00:00'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable values('2017-03-02 01:10:20'); Query OK, 1 row affected (10.19 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将生成以下输出 −
+---------------------+ | Duetimestamp | +---------------------+ | 2019-08-20 20:48:45 | | 2016-01-21 12:34:00 | | 2018-05-01 02:00:00 | | 2017-03-02 01:10:20 | +---------------------+ 4 rows in set (0.00 sec)
以下是将 MySQL 时间戳转换为 UNIX 时间戳的查询 −
mysql> select unix_timestamp(Duetimestamp) from DemoTable;
这将生成以下输出 −
+------------------------------+ | unix_timestamp(Duetimestamp) | +------------------------------+ | 1566314325 | | 1453359840 | | 1525120200 | | 1488397220 | +------------------------------+ 4 rows in set (0.04 sec)
广告