MySQL中将日期时间转换为整数? 您所在的位置:网站首页 mysql时间转换为时间戳 MySQL中将日期时间转换为整数?

MySQL中将日期时间转换为整数?

2023-06-23 09:19| 来源: 网络整理| 查看: 265

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)

使用插入命令向表格中插入一些记录。查询语句如下:

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)

阅读更多:MySQL 教程



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有