MySQL将时间minuts_seconds转换为秒

时间:2016-04-07 18:00:51

标签: php mysql time

嗨其他溢出用户,我一直在寻找如何使用MySQL命令将给定时间作为HHhMMmssS ex:28m54s or 1h22m3s or 27s转换为纯秒。

输出应该是纯秒,如:

from: 28m54s  -> 1734
from: 1h22m3s  ->  4923
from: 27s  ->  27

这些值已导入名为length的列。

如果无法做到这一点,那么第二个最佳选择就是PHP。这是一个工作,所以复制粘贴答案可以做:)

MariaDB结构

  `title` varchar(255) NOT NULL DEFAULT '',
  `embed` varchar(800) NOT NULL,
  `description` text NOT NULL,
  `keywords` varchar(255) NOT NULL,
  `length` int(11) NOT NULL DEFAULT '0',
  `source_thumb_url` varchar(255) NOT NULL,
  `record_num` int(11) NOT NULL AUTO_INCREMENT

MariaDB [csv_import]> SHOW COLUMNS FROM table_name;
+------------------+--------------+------+-----+---------+-------+
| Field            | Type         | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| title            | varchar(255) | NO   |     | NULL    |       |
| embed            | varchar(800) | NO   |     | NULL    |       |
| description      | text         | NO   |     | NULL    |       |
| keywords         | varchar(255) | NO   |     | NULL    |       |
| length           | int(11)      | NO   |     | NULL    |       |
| source_thumb_url | varchar(255) | NO   |     | NULL    |       |
| record_num       | int(11)      | NO   | PRI | NULL    |       |
+------------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

选择字符串

SET @from = '`length`';
SELECT TIME_TO_SEC(STR_TO_DATE(
    CONCAT(IF(@from LIKE '%h%', '', '0h'), IF(@from LIKE '%m%', '', '0m'), IF(@from LIKE '%s%', '', '0s'), @from),
    '%kh%im%ss')) AS sec;

使用UPDTAE的结果

UPDATE `database_name`.`table_name` SET `length` = SELECT TIME_TO_SEC(STR_TO_DATE(
    CONCAT(IF(`length` LIKE '%h%', '', '0h'), IF(`length` LIKE '%m%', '', '0m'), `length`),
    '%kh%im%ss'));

返回:错误代码:1064 您的SQL语法有错误;检查与MariaDB服务器版本对应的手册,以便在'select time_to_sec(str_to_date(str_to_date)附近使用正确的语法(str_to_date(     CONCAT(IF(length喜欢'%h%','','0h'),我在第1行

并对表格长度进行了更改

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| title            | varchar(255) | NO   |     | NULL    |                |
| embed            | varchar(800) | NO   |     | NULL    |                |
| description      | text         | NO   |     | NULL    |                |
| keywords         | varchar(255) | NO   |     | NULL    |                |
| length           | varchar(20)  | NO   |     | NULL    |                |
| source_thumb_url | varchar(255) | NO   |     | NULL    |                |
| record_num       | int(11)      | NO   | PRI | NULL    | auto_increment |
+------------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

更新草莓的回答

SELECT CASE WHEN `length` LIKE '%h%' 
            THEN TIME_TO_SEC(STR_TO_DATE(`length`,'%hh%im%ss')) 
            WHEN `length` LIKE '%m%' 
            THEN TIME_TO_SEC(STR_TO_DATE(`length`,'%im%ss')) 
            ELSE TIME_TO_SEC(STR_TO_DATE(`length`,'%ss')) 
            END X;

错误代码:1054 “字段列表”中的未知列“长度”

2 个答案:

答案 0 :(得分:3)

使用time_to_secstr_to_date

使用以下查询
select time_to_sec(str_to_date(
    CONCAT(IF(@from like '%h%', '', '0h'), IF(@from like '%m%', '', '0m'), @from),
    '%kh%im%ss')) as sec;

演示:

mysql> set @from = '28m54s';
Query OK, 0 rows affected (0.00 sec)
mysql> select time_to_sec(str_to_date(
    ->     CONCAT(IF(@from like '%h%', '', '0h'), IF(@from like '%m%', '', '0m'), @from),
    ->     '%kh%im%ss')) as sec;
+------+
| sec  |
+------+
| 1734 |
+------+
1 row in set (0.00 sec)

mysql> set @from = '1h22m3s';
Query OK, 0 rows affected (0.00 sec)
mysql> select time_to_sec(str_to_date(
    ->     CONCAT(IF(@from like '%h%', '', '0h'), IF(@from like '%m%', '', '0m'), @from),
    ->     '%kh%im%ss')) as sec;
+------+
| sec  |
+------+
| 4923 |
+------+
1 row in set (0.00 sec)

mysql> set @from = '27s';
Query OK, 0 rows affected (0.00 sec)
mysql> select time_to_sec(str_to_date(
    ->     CONCAT(IF(@from like '%h%', '', '0h'), IF(@from like '%m%', '', '0m'), @from),
    ->     '%kh%im%ss')) as sec;
+------+
| sec  |
+------+
|   27 |
+------+
1 row in set (0.00 sec)

根据提供的表格结构,您可能需要将列length的类型从int(11)更改为varchar(20)

新结构可能是这样的:

  `title` varchar(255) NOT NULL DEFAULT '',
  `embed` varchar(800) NOT NULL,
  `description` text NOT NULL,
  `keywords` varchar(255) NOT NULL,
  `length` varchar(20) NOT NULL DEFAULT '0',
  `source_thumb_url` varchar(255) NOT NULL,
  `record_num` int(11) NOT NULL AUTO_INCREMENT

然后在导入后,使用以下update进行所需的转换。

update table_name 
set `length` = time_to_sec(str_to_date(
    CONCAT(IF(`length` like '%h%', '', '0h'), IF(`length` like '%m%', '', '0m'), `length`),
    '%kh%im%ss'));

答案 1 :(得分:0)

假设长度不是一个整数......

SELECT CASE WHEN length LIKE '%h%' 
            THEN TIME_TO_SEC(STR_TO_DATE(length ,'%hh%im%ss')) 
            WHEN length LIKE '%m%' 
            THEN TIME_TO_SEC(STR_TO_DATE(length ,'%im%ss')) 
            ELSE TIME_TO_SEC(STR_TO_DATE(length ,'%ss')) 
        END x
  FROM my_table;