在 MySQL 中将日期时间转换成整数?
在 MySQL 中,使用 UNIX_TIMESTAMP() 函数将日期时间转换成整数。语法如下
SELECT UNIX_TIMESTAMP(yourDatetimeColumnName) as anyVariableName FROM yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table DatetimeToInteger -> ( -> Id int NOT NULL AUTO_INCREMENT, -> ArrivalTime datetime, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.94 sec)
使用 insert 命令向表中插入一些记录。查询如下
mysql> insert into DatetimeToInteger(ArrivalTime) values(now());
Query OK, 1 row affected (0.09 sec)
mysql> insert into DatetimeToInteger(ArrivalTime) values(curdate());
Query OK, 1 row affected (0.21 sec)
mysql> insert into DatetimeToInteger(ArrivalTime) values('2017-09-21 13:10:55');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DatetimeToInteger(ArrivalTime) values(date_add(now(),interval 3 year));
Query OK, 1 row affected (0.16 sec)使用 select 语句显示表中的所有记录。查询如下
mysql> select *from DatetimeToInteger;
输出如下
+----+---------------------+ | Id | ArrivalTime | +----+---------------------+ | 1 | 2019-01-22 15:12:45 | | 2 | 2019-01-22 00:00:00 | | 3 | 2017-09-21 13:10:55 | | 4 | 2022-01-22 15:14:32 | +----+---------------------+ 4 rows in set (0.00 sec)
以下是将日期时间转换成整数的查询
mysql> select unix_timestamp(ArrivalTime) as DateTimeToIntegerDemo from DatetimeToInteger;
输出如下
+-----------------------+ | DateTimeToIntegerDemo | +-----------------------+ | 1548150165 | | 1548095400 | | 1505979655 | | 1642844672 | +-----------------------+ 4 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP