MySQL 查询将"1h 15 min"这样的字符串转换为 75 分钟?
mysqlmysqli database更新于 2025/8/12 8:12:17
您可以使用 str_to_date() 进行此转换。让我们首先创建一个表 −
mysql> create table DemoTable ( stringDate varchar(100) ); Query OK, 0 rows affected (0.86 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('1h 15 min');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values('2h 30 min');
Query OK, 1 row affected (0.21 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+------------+ | stringDate | +------------+ | 1h 15 min | | 2h 30 min | +------------+ 2 rows in set (0.00 se
以下查询将字符串"1 小时 15 分钟"转换为 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

