【问题标题】:Split and compare hour format in MySQL在 MySQL 中拆分和比较小时格式
【发布时间】:2019-07-23 03:27:45
【问题描述】:

我正在继续其他人的项目,他们将时间以hh:mm/hh:mm 格式存储在 MySQL 数据库中,并带有分隔符 /

如何使用 TIME 数据类型划分和比较这些时间?

【问题讨论】:

标签: mysql sql time mysqljs


【解决方案1】:

考虑到字段的数量是有限且已知的,您可以使用这个(诚然)丑陋的解决方案来使用 SUBSTRING_INDEX 拆分字段并将它们转换为 TIME 以进行进一步比较/操作。

SELECT
    CAST(SUBSTRING_INDEX(monday,'/',1) AS TIME) AS 'monday_start',
    CAST(SUBSTRING_INDEX(monday,'/',-1) AS TIME) AS 'monday_end',
    CAST(SUBSTRING_INDEX(tuesday,'/',1) AS TIME) AS 'tuesday_start',
    CAST(SUBSTRING_INDEX(tuesday,'/',-1) AS TIME) AS 'tuesday_end',
    CAST(SUBSTRING_INDEX(wednesday,'/',1) AS TIME) AS 'wednesday_start',
    CAST(SUBSTRING_INDEX(wednesday,'/',-1) AS TIME) AS 'wednesday_end',
    CAST(SUBSTRING_INDEX(thursday,'/',1) AS TIME) AS 'thursday_start',
    CAST(SUBSTRING_INDEX(thursday,'/',-1) AS TIME) AS 'thursday_end',
    CAST(SUBSTRING_INDEX(friday,'/',1) AS TIME) AS 'friday_start',
    CAST(SUBSTRING_INDEX(friday,'/',-1) AS TIME) AS 'friday_end',
    CAST(SUBSTRING_INDEX(saturday,'/',1) AS TIME) AS 'saturday_start',
    CAST(SUBSTRING_INDEX(saturday,'/',-1) AS TIME) AS 'saturday_end',
    CAST(SUBSTRING_INDEX(sunday,'/',1) AS TIME) AS 'sunday_start',
    CAST(SUBSTRING_INDEX(sunday,'/',-1) AS TIME) AS 'sunday_end'
FROM times

DB Fiddle

an older answer 中向 Stack Overflow 用户 madde74 致敬。

【讨论】:

    【解决方案2】:

    这会将星期四列的第一次拆分并转换为数据类型TIME

    SELECT TIME(SUBSTRING_INDEX(thursday,'/',1)) FROM YOUR_TABLE
    

    输出

    03:01:00
    

    【讨论】:

      【解决方案3】:

      希望你能完成剩下的……

      create database t
      
         create table t (
         user_id int,
         dayofweek varchar(15),
         timerange varchar(15)
         )
      
         insert into t (user_id, dayofweek, timerange) select 83, 'monday', '00:00/12:30'
         <br>insert into t (user_id, dayofweek, timerange) select 83, 'tuesday', null
         <br>insert into t (user_id, dayofweek, timerange) select 83, 'wednesday', '00:00/24:00'
         <br>insert into t (user_id, dayofweek, timerange) select 83, 'thursday', '03:01/10:02'
         <br>insert into t (user_id, dayofweek, timerange) select 83, 'friday', '00:00/24:00'
         <br>insert into t (user_id, dayofweek, timerange) select 83, 'saturday', '00:00/24:00'
         <br>insert into t (user_id, dayofweek, timerange) select 83, 'sunday', '00:00/24:00'
      
      select *, 
      substring(timerange, 1, 2) as [timerange1hours], 
      substring(timerange, 4, 2) as [timerange1minutes], 
      substring(timerange, 7, 2) as [timerange2hours], 
      substring(timerange, 10, 2) as [timerange2minutes],
      convert(int, substring(timerange, 7, 2)) - convert(int, substring(timerange, 1, 2)) as [diffhours],
      
      convert(int, substring(timerange, 10, 2)) - convert(int, substring(timerange, 4, 2)) as [diffminutes]
      from t 
      where timerange is not null
      
      
      delete  from t
      

      【讨论】:

      • 83 星期一 00:00/12:30 00 00 12 30 12 30 83 星期三 00:00/24:00 00 00 24 00 24 0 83 星期四 03:01/10:02 03 01 10 02 7 1 83 周五 00:00/24:00 00 00 24 00 24 0 83 周六 00:00/24:00 00 00 24 00 24 0 83 周日 00:00/24:00 00 00 24 00 24 0跨度>
      【解决方案4】:

      根据您的解释,我假设“/”两侧的两个时间元素是时间和时间。换句话说,monday 列可以分为 monday_from 和 monday_to 列,然后 00:00/12:30 可以拆分为 monday_from 具有 00:00 和 monday_to 具有 12:30。

      以下步骤将帮助您实现目标

      • 你可以使用SUBSTRING_INDEX()分割00:00/12:30

      例如select SUBSTRING_INDEX('00:00/12:30', '/', 1) monday_to, SUBSTRING_INDEX('00:00/12:30', '/', -1) monday_from

      • 然后您可以使用CONCAT() 将:00 附加到每个以表示秒。这将是微不足道的,因为没有记录数据
      • 使用str_to_date()函数可以将这个字符串转换为时间

      例如select str_to_date('12:30:00', %h:%i:%s) - 然后您可以将这些作为时间进行比较

      select 
      str_to_date(CONCAT(SUBSTRING_INDEX('00:00/12:30', '/', 1), ':00'), %h:%i:%s) as monday_to, 
      str_to_date(CONCAT(SUBSTRING_INDEX('00:00/12:30', '/', -1), ':00'), %h:%i:%s) as monday_from
      

      注意:我手边没有 MySQL 数据库,所以没有检查 SQL 的语法错误,可能有一些小但可纠正的错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多