从MySQL中选择并显示营业时间

时间:2022-06-07 01:26:54

I dont need to check if business is open or close, but I need to show open hours by days.

我不需要检查业务是开放还是关闭,但我需要按天显示营业时间。

There are some options:

有一些选择:

1 - Business open once in day (sample - from 10:00 to 18:30) - one rows in table 2 - Business open TWICE in day (samlpe - from 10:00 to 14:00 and from 15:00 to 18:30) - two rows in table 3 - Business may be closed (no row inserted)

1 - 商务开放时间为一天(样本 - 从10:00到18:30) - 表2中的一行 - 商业开放时间为两天(samlpe - 从10:00到14:00和15:00到18: 30) - 表3中的两行 - 业务可能已关闭(未插入行)

Here my MySql table of hours storing. In this sample business (affiliate_id) are open twice in days from 0 to 4, once in day 5 and closed in day 6 (no records for this day)

这里我的MySql表存放时间。在此示例中,business(affiliate_id)在0到4天内开放两次,在第5天开放一次,在第6天关闭(当天没有记录)

http://postimage.org/image/yplj4rumj/

What I need to show in website its like (according to this database example:

我需要在网站上显示它(根据这个数据库示例:

0,1,2,3,4 - open 10:00-14:00 and 15:00-18:30 5 - open 10:00-12:00 6 - closed

0,1,2,3,4 - 开放时间为10:00-14:00和15:00-18:30 5 - 营业时间为10:00-12:00 6 - 已关闭

How I get results like:

我如何获得如下结果:

http://postimage.org/image/toe53en63/

?

I tried to make queries with GROUPֹ_CONCAT and LEFT JOIN the same table ON a.day=b.day but with no luck :(

我尝试用GROUPֹ_CONCAT和LEFT JOIN在a.day = b.day上同一个表进行查询,但没有运气:(

There sample of my query (that is wrong)

有我的查询样本(这是错的)

SELECT GROUP_CONCAT( DISTINCT CAST( a.day AS CHAR ) 
ORDER BY a.day ) AS days, DATE_FORMAT( a.time_from,  '%H:%i' ) AS f_time_from, DATE_FORMAT( a.time_to,  '%H:%i' ) AS f_time_to, DATE_FORMAT( b.time_from, '%H:%i' ) AS f_time_from_s, DATE_FORMAT( b.time_to,  '%H:%i' ) AS f_time_to_s
FROM business_affiliate_hours AS a LEFT 
JOIN business_affiliate_hours AS b ON a.day = b.day
WHERE a.affiliate_id =57

GROUP BY a.time_from, a.time_to, b.time_from, b.time_to

 ORDER BY a.id ASC

This my table:

这是我的表:

CREATE TABLE IF NOT EXISTS `business_affiliate_hours` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `affiliate_id` int(10) unsigned NOT NULL DEFAULT '0',
  `time_from` time NOT NULL,
  `time_to` time NOT NULL,
  `day` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;


INSERT INTO `business_affiliate_hours` (`id`, `affiliate_id`, `time_from`, `time_to`, `day`) VALUES
(53, 57, '10:00:00', '12:00:00', 5),
(52, 57, '15:00:00', '18:30:00', 4),
(51, 57, '10:00:00', '14:00:00', 4),
(50, 57, '15:00:00', '18:30:00', 3),
(49, 57, '10:00:00', '14:00:00', 3),
(48, 57, '15:00:00', '18:30:00', 2),
(47, 57, '10:00:00', '14:00:00', 2),
(46, 57, '15:00:00', '18:30:00', 1),
(45, 57, '10:00:00', '14:00:00', 1),
(44, 57, '15:00:00', '18:30:00', 0),
(43, 57, '10:00:00', '14:00:00', 0);

Open hours may be different for every day, so I want to GROUP by the same open hours, and get list of days for all unique order of open hours.

每天的营业时间可能会有所不同,所以我希望在相同的营业时间内分组,并获得所有独特的营业时间顺序的天数列表。

Need your help!

需要你的帮助!

Sorry for links to images, I cant upload images yes to here.

很抱歉有图片的链接,我无法将图片上传到此处。

1 个解决方案

#1


2  

First build a materialised table of each day's combined times, then group on that:

首先构建每天合并时间的物化表,然后分组:

SELECT   GROUP_CONCAT(day ORDER BY day) AS days,
         DATE_FORMAT(f1, '%H:%i') AS f_time_from,
         DATE_FORMAT(t1, '%H:%i') AS f_time_to,
         DATE_FORMAT(f2, '%H:%i') AS f_time_from_s,
         DATE_FORMAT(t2, '%H:%i') AS f_time_to_s
FROM (
  SELECT   day,
           MIN(time_from) AS f1,
           MIN(time_to  ) AS t1,
           IF(COUNT(*) > 1, MAX(time_from), NULL) AS f2,
           IF(COUNT(*) > 1, MAX(time_to  ), NULL) AS t2
  FROM     business_affiliate_hours
  WHERE    affiliate_id = 57
  GROUP BY day
) t
GROUP BY f1, t1, f2, t2
ORDER BY days

See it on sqlfiddle.

在sqlfiddle上看到它。

#1


2  

First build a materialised table of each day's combined times, then group on that:

首先构建每天合并时间的物化表,然后分组:

SELECT   GROUP_CONCAT(day ORDER BY day) AS days,
         DATE_FORMAT(f1, '%H:%i') AS f_time_from,
         DATE_FORMAT(t1, '%H:%i') AS f_time_to,
         DATE_FORMAT(f2, '%H:%i') AS f_time_from_s,
         DATE_FORMAT(t2, '%H:%i') AS f_time_to_s
FROM (
  SELECT   day,
           MIN(time_from) AS f1,
           MIN(time_to  ) AS t1,
           IF(COUNT(*) > 1, MAX(time_from), NULL) AS f2,
           IF(COUNT(*) > 1, MAX(time_to  ), NULL) AS t2
  FROM     business_affiliate_hours
  WHERE    affiliate_id = 57
  GROUP BY day
) t
GROUP BY f1, t1, f2, t2
ORDER BY days

See it on sqlfiddle.

在sqlfiddle上看到它。