Reputation: 1697
I need a query to check if the business has business hours set for EACH day of the week (dayofweek IDs
are 1-7)
It will be used something like this:
hours_check = MySQL query will check if hours exists for each day of the week
if hours_check true:
return X
else:
return Y
I have these 3 tables:
BUSINESS
id ..
name ..
DAYOFWEEK
id ..
name ..
HOURS
id ..
business_id ..
dayofweek_id ..
I know how to check for one day with something like:
SELECT EXISTS(SELECT id FROM hours WHERE business_id = 1 and dayofweek_id = 1)
How do I check for each day without running a separate query for each day?
Upvotes: 1
Views: 90
Reputation: 5730
Try this:
create table BUSINESS ( id int NOT NULL AUTO_INCREMENT primary key,
name char(20) );
create table DAYOFWEEK ( id int not NULL,
name char(3) );
create table HOURS ( id int NOT NULL AUTO_INCREMENT primary key,
business_id int NOT NULL references BUSINESS(id),
dayofweek_id int NOT NULL references DAYOFWEEK(id),
hour_range char(20) );
insert into BUSINESS (id, name) values (1000, 'Walmart'),
(1001, 'RadioShack'),
(1002, 'AlwaysClosed');
insert into DAYOFWEEK (id, name) values
(1,'Mon'), (2,'Tue'), (3,'Wed'), (4,'Tue'),
(5,'Fri'), (6,'Sat'), (7,'Sun');
insert into HOURS (business_id, dayofweek_id, hour_range) values
(1000, 2, '08:00 - 10:00'),
(1000, 2, '12:00 - 19:00'),
(1000, 6, '09:00 - 21:00'),
(1001, 3, '07:00 - 17:00');
-- Get an overview which shop is open when:
select b.name, d.name as day, h.hour_range
from HOURS h inner join DAYOFWEEK d on h.dayofweek_id=d.id
left join BUSINESS b on b.id=h.business_id
order by b.name, d.id, h.hour_range;
-- Get an overview for each day:
select d.name as day, b.name, h.hour_range
from DAYOFWEEK d left join HOURS h on h.dayofweek_id=d.id
left join BUSINESS b on b.id=h.business_id
order by d.id, b.name, h.hour_range;
The two selects will result in
+------------+------+---------------+
| name | day | hour_range |
+------------+------+---------------+
| RadioShack | Wed | 07:00 - 17:00 |
| Walmart | Tue | 08:00 - 10:00 |
| Walmart | Tue | 12:00 - 19:00 |
| Walmart | Sat | 09:00 - 21:00 |
+------------+------+---------------+
4 rows in set (0.00 sec)
and
+------+------------+---------------+
| day | name | hour_range |
+------+------------+---------------+
| Mon | NULL | NULL |
| Tue | Walmart | 08:00 - 10:00 |
| Tue | Walmart | 12:00 - 19:00 |
| Wed | RadioShack | 07:00 - 17:00 |
| Tue | NULL | NULL |
| Fri | NULL | NULL |
| Sat | Walmart | 09:00 - 21:00 |
| Sun | NULL | NULL |
+------+------------+---------------+
8 rows in set (0.00 sec)
Upvotes: 2
Reputation: 61
You could use the SQL IN operator.
SELECT
EXISTS(
SELECT id FROM hours WHERE business_id = 1 and dayofweek_id IN [1,2,3..etc]
)
Assuming you just want to make sure that there exists an hour during the week that they are open.
Upvotes: 1
Reputation: 162
Something like this:
select hours.*, dayofweek.name AS day
from dayofweek
inner join hours on hours.dayofweek_id = dayofweek.id
where hours.business_id = 1
Upvotes: 0