2

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)

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)

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

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 :(

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 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.