mysql按范围内的打开和关闭时间选择和分组

nafvub8i  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(436)

我有一张table,上面有餐厅的营业时间和关门时间。我需要一个sql查询来选择一个特定业务的开放或关闭状态范围内的日期。
表格:

  1. id tbl_index_id dow status starttime endtime
  2. 1 1 Monday Closed
  3. 2 1 Tuesday Open 8.00 17.00
  4. 3 1 Wednesday Open 8.00 17.00
  5. 4 1 Thursday Open 8.00 17.00
  6. 5 1 Friday Open 8.00 17.00
  7. 6 1 Saturday Open 8.00 17.00
  8. 7 1 Sunday Closed

tabl\u index\u id是餐厅的外键。我知道,名字很好笑,但这是有原因的。
我需要的是:
开放时间:周二-周六:08:00 am-05:00 pm周六和周日:关闭
我能想到的唯一解决办法是:

  1. SELECT DoW as openDays, start_time, end_time from tbl_businesshrs WHERE
  2. tbl_businesshrs.tbl_index_id = 1 and status = 'Open' GROUP BY DoW

请帮忙。我真的搞不懂这个。

cu6pst1q

cu6pst1q1#

这就是我提出的解决方案:

  1. SELECT (SELECT LEFT(DoW,3) from tbl_businesshrs where tbl_businesshrs.id =
  2. (SELECT MIN(tbl_businesshrs.id) FROM tbl_businesshrs WHERE
  3. tbl_businesshrs.tbl_index_id=1 AND tbl_businesshrs.status='Open')) as
  4. fistDay,
  5. (SELECT LEFT(DoW,3) from tbl_businesshrs where tbl_businesshrs.id = (SELECT
  6. MAX(tbl_businesshrs.id) FROM tbl_businesshrs WHERE
  7. tbl_businesshrs.tbl_index_id=1 AND tbl_businesshrs.status='Open')) as
  8. lastDay,
  9. start_time, end_time from tbl_businesshrs WHERE tbl_businesshrs.tbl_index_id
  10. =
  11. 1 and status = 'Open' GROUP BY status ORDER BY id ASC

以及输出:

4xy9mtcn

4xy9mtcn2#

一个通用的解决方案有点棘手,但只是为了好玩,这里有一个基于派生的复合“timekey”使用变量来计算块数和行号,然后连接

  1. drop table if exists tbl_businesshrs;
  2. create table tbl_businesshrs(id int auto_increment primary key,
  3. tbl_index_id int,
  4. dow varchar(3), status varchar(6), start_time time, end_time time);
  5. insert into tbl_businesshrs (dow,tbl_index_id, status,start_time, end_time) values
  6. ('mon',1,'closed',null,null),
  7. ('tue',1,'open','08:00','17:00'),
  8. ('wed',1,'open','08:00','17:00'),
  9. ('thu',1,'open','05:00','17:00'),
  10. ('fri',1,'open','08:00','16:00'),
  11. ('sat',1,'open','08:00','17:00'),
  12. ('sun',1,'closed',null,null);
  13. select
  14. concat('open ',
  15. group_concat(
  16. case when c.dow = d.dow then concat(c.dow,' ' ,c.start_time,'-',c.end_time)
  17. else concat(c.dow,' to ',d.dow, ' ', c.start_time, '-',c.end_time)
  18. end
  19. )
  20. ) open,
  21. (select group_concat(dow) from tbl_businesshrs t where t.status = 'closed') closed
  22. from
  23. (
  24. select * from
  25. (
  26. select id,dow,tbl_index_id,status,start_time,end_time, concat(start_time,end_time) timekey,
  27. if (concat(start_time,end_time) <> @timekey, @bn:=@bn+1,@bn:=@bn) bn,
  28. if (concat(start_time,end_time) <> @timekey, @rn:=1,@rn:=@rn + 1) rn,
  29. @timekey:=concat(coalesce(start_time,'00:00:00'),coalesce(end_time,'00:00:00')) tk
  30. from tbl_businesshrs t
  31. cross join
  32. (select @bn:=0,@rn:=0,@timekey:= concat(cast('00:00:00' as time),cast('00:00:00' as time))) r
  33. where status = 'open'
  34. order by id
  35. ) a
  36. where rn = 1
  37. ) c
  38. join
  39. (
  40. select b.*,
  41. if(b.bn <> @p ,@rn:=1,@rn:=@rn+1) rn,
  42. @p:=b.bn p
  43. from
  44. (
  45. select id,dow,tbl_index_id,status,start_time,end_time, concat(start_time,end_time) timekey,
  46. if (concat(start_time,end_time) <> @timekey1, @bn1:=@bn1+1,@bn1:=@bn1) bn,
  47. #if (concat(start_time,end_time) <> @timekey1, @rn1:=1,@rn1:=@rn1+1) rn,
  48. @timekey1:=concat(coalesce(start_time,'00:00:00'),coalesce(end_time,'00:00:00')) tk
  49. from tbl_businesshrs t
  50. cross join
  51. (select @bn1:=0,
  52. # @rn1:=0,
  53. @timekey1:= concat(cast('00:00:00' as time),cast('00:00:00' as time))) r
  54. where status = 'open'
  55. order by id
  56. ) b
  57. cross join(select @p:=0,@rn:=0) r
  58. order by b.bn ,b.id desc
  59. ) d
  60. on d.bn = c.bn
  61. where d.rn = c.rn
  62. ;
  63. +-----------------------------------------------------------------------------------------------------+---------+
  64. | open | closed |
  65. +-----------------------------------------------------------------------------------------------------+---------+
  66. | open tue to wed 08:00:00-17:00:00,thu 05:00:00-17:00:00,fri 08:00:00-16:00:00,sat 08:00:00-17:00:00 | mon,sun |
  67. +-----------------------------------------------------------------------------------------------------+---------+
  68. 1 row in set (0.12 sec)

还有你的样本数据

  1. +-----------------------------------+---------+
  2. | open | closed |
  3. +-----------------------------------+---------+
  4. | open tue to sat 08:00:00-17:00:00 | mon,sun |
  5. +-----------------------------------+---------+
  6. 1 row in set (0.00 sec)

注意,我们总是将块号与块号连接起来,行号=1

展开查看全部

相关问题