1

我正在使用 MySQL 版本 5.7.28。我有如下的 json 数据。

CREATE TABLE `week2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` smallint(1),
  `json` text ,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO week2(id,type,json)
VALUES
    (121,1,'[{"weekdays":"Sunday"},{"weekdays":"Monday"},{"weekdays":"Tuesday"},{"weekdays":"Wednesday"},{"weekdays":"Thursday"},{"weekdays":"Friday"},{"weekdays":"Saturday"}]'),
    (122,1,'[{"weekdays":"Sunday"},{"weekdays":"Monday"}]'),
    (123,2,'[{"start_time":"08:00 AM","end_time":"10:00 PM"}]');

如您所见, json 列具有嵌套的 JSON 数据。所以在这里我想比较今天(这是星期六,我们目前处于开始时间和结束时间之间)

预期结果:

(121,1,'[{"weekdays":"Sunday"},{"weekdays":"Monday"},{"weekdays":"Tuesday"},{"weekdays":"Wednesday"},{"weekdays":"Thursday"},{"weekdays":"Friday"},{"weekdays":"Saturday"}]'),
(123,2,'[{"start_time":"08:00 AM","end_time":"10:00 PM"}]');
4

2 回答 2

1

您需要检测日期名称(1)和时间段(2) ,并在最后通过OR运算符组合这两个条件。

For (1):使用函数检测当天的名称,DAYNAME()并通过函数查找包含工作日键的JSON数据中是否存在JSON_CONTAINS

For (2) :数据类型的TIME()函数和CASTing 字符串TIME可能与一个技巧一起使用,为PM类型时间的情况添加 12 小时的迭代。

所以,考虑使用:

SELECT *
  FROM `week2`
 WHERE JSON_CONTAINS(`json`->>'$[*].weekdays', CONCAT('"',DAYNAME( NOW() ),'"')) = 1
    OR
    (
      TIME(ADDTIME(NOW(),"8:00:00")) >=       
      CASE WHEN INSTR(REPLACE(`json`->>'$[0].start_time',"12:00 AM","00:00 AM"),"PM")>0 
           THEN 
                CAST(CONCAT(MOD((TIME_FORMAT(REPLACE(`json`->>'$[0].start_time',"12:00 AM","00:00 AM"), "%T")+12),24),":00 AM") 
                  AS TIME) 
           ELSE
                CAST((REPLACE(`json`->>'$[0].start_time',"12:00 AM","00:00 AM")) AS TIME)
            END    
   AND 
      TIME(ADDTIME(NOW(),"8:00:00")) <=
           CASE WHEN INSTR(REPLACE(`json`->>'$[0].end_time',"12:00 AM","00:00 AM"),"PM")>0 
           THEN 
                CAST(CONCAT(MOD((TIME_FORMAT(REPLACE(`json`->>'$[0].end_time',"12:00 AM","00:00 AM"), "%T")+12),24),":00 AM") 
                  AS TIME) 
           ELSE
                CAST(REPLACE(`json`->>'$[0].end_time',"12:00 AM","00:00 AM") AS TIME)                  
            END)

Demo

于 2020-06-27T10:54:17.223 回答
0

也许,是这样的:

SELECT *,CURRENT_TIME 
  FROM
(SELECT *,JSON_UNQUOTE(JSON_EXTRACT(`json`,'$[0].start_time')) AS st,
       JSON_UNQUOTE(JSON_EXTRACT(`json`,'$[0].end_time')) AS et FROM week2) V 
WHERE (JSON_SEARCH(`json`, 'one', DAYNAME(CURDATE()))  IS NOT NULL
       OR
     CURRENT_TIME hour >= 
      CASE WHEN st LIKE '%AM%' THEN REPLACE(st,' AM',':00') 
            WHEN st LIKE '%PM%' THEN SEC_TO_TIME(TIME_TO_SEC(REPLACE(st,' PM',':00'))+43200)
            END
      AND
      CURRENT_TIME hour <=
      CASE WHEN et LIKE '%AM%' THEN REPLACE(et,' AM',':00') 
            WHEN et LIKE '%PM%' THEN SEC_TO_TIME(TIME_TO_SEC(REPLACE(et,' PM',':00'))+43200)
            END);

第一个查询是使用JSON_EXTRACT提取时间和JSON_UNQUOTE删除 ( ") 然后将其作为子查询。

在外部查询中,使用JSON_SEARCH与今天的;比较找到weekdays值 如果没有找到它,它将返回,因此使用条件。DAYNAME(CURDATE())JSON_SEARCHNULLIS NOT NULL

追加OR以与从字段CURRENT_TIME中提取的时间值进行比较。json但首先,使用CASE表达式来确定它是否是AMor PM; 如果是,则AM只是带有秒针 ( )。如果是,请执行类似操作,然后使用将时间值转换为秒,然后添加 43200 秒(12 小时)并使用 .再次转换为标准格式。REPLACE AM:00PMREPLACEAMTIME_TO_SEChour:minute:secondSEC_TO_TIME

P/S:这是我试图回答但我没有发布它,因为我得到了错误的时间结果。只有当我放入CURRENT_TIME小提琴时,我才意识到小提琴时区与我的不同。另外,我正在本地数据库上进行测试;这是 MariaDB,使用将字符串转换为时间的结果STR_TO_TIME是不同的。老实说,在我看来,如果这是 MariaDB,我认为查询可能会更短。

于 2020-06-29T09:10:32.097 回答