要将MM / DD / YY转换为UNIX时间戳,可以使用以下语法-
select UNIX_TIMESTAMP(str_to_date(yourColumnName,'%m/%d/%Y')) from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, dateConvertToUnix varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(dateConvertToUnix) values('01/10/2001'); mysql> insert into DemoTable(dateConvertToUnix) values('03/31/2010'); mysql> insert into DemoTable(dateConvertToUnix) values('12/31/2016'); mysql> insert into DemoTable(dateConvertToUnix) values('04/27/2019');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+----+-------------------+ | Id | dateConvertToUnix | +----+-------------------+ | 1 | 01/10/2001 | | 2 | 03/31/2010 | | 3 | 12/31/2016 | | 4 | 04/27/2019 | +----+-------------------+ 4 rows in set (0.00 sec)
以下是将MM / DD / YY转换为UNIX时间戳的查询-
mysql> select UNIX_TIMESTAMP(str_to_date(dateConvertToUnix,'%m/%d/%Y')) from DemoTable;
这将产生以下输出-
+-----------------------------------------------------------+ | UNIX_TIMESTAMP(str_to_date(dateConvertToUnix,'%m/%d/%Y')) | +-----------------------------------------------------------+ | 979065000 | | 1269973800 | | 1483122600 | | 1556303400 | +-----------------------------------------------------------+ 4 rows in set (0.00 sec)