您可以使用str_to_date()进行此转换。让我们首先创建一个表-
mysql> create table DemoTable ( stringDate varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('1h 15 min'); mysql> insert into DemoTable values('2h 30 min');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+------------+ | stringDate | +------------+ | 1h 15 min | | 2h 30 min | +------------+ 2 rows in set (0.00 se
以下是将字符串如“ 1 h 15 min”的字符串转换为75即75分钟的查询-
mysql> select time_to_sec(str_to_date(stringDate, '%l h %i min')) / 60 second from DemoTable having second is not null union all select time_to_sec(str_to_date(stringDate, '%i min')) / 60 second from DemoTable having second is not null;
输出结果
+----------+ | second | +----------+ | 75.0000 | | 150.0000 | +----------+ 2 rows in set, 2 warnings (0.04 sec)