-3

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

如何将这些时间与TIME数据类型分开并进行比较?

在此处输入图像描述

4

4 回答 4

2

考虑到字段的数量是有限且已知的,您可以使用这个(诚然)丑陋的解决方案来拆分字段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小提琴

给 Stack Overflow 用户 madde74 的提示,以获取旧答案中的灵感。

于 2019-07-23T03:57:09.373 回答
2

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

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

输出

03:01:00
于 2019-07-23T04:02:25.840 回答
2

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

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

  • 您可以使用拆分 00:00/12:30SUBSTRING_INDEX()

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

  • 然后,您可以将 :00 附加到每个以使用CONCAT(). 这将是微不足道的,因为没有记录数据
  • 使用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 DB,所以没有检查 SQL 的语法错误,可能有小但可纠正的错误。

于 2019-07-23T04:07:34.297 回答
2

我希望你能完成剩下的...

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
于 2019-07-23T04:14:03.650 回答