MySQL检查一周中每天的营业时间是否存在

时间:2016-01-29 18:34:32

标签: python mysql django

我需要一个查询来检查商家是否为每个一周中设置营业时间(周日IDs为1-7)

它将使用类似的东西:

hours_check = MySQL query will check if hours exists for each day of the week

if hours_check true:
    return X 
else:
    return Y

我有这3张桌子:

BUSINESS
id ..
name ..

DAYOFWEEK
id ..
name ..

HOURS
id ..
business_id ..
dayofweek_id .. 

我知道如何用以下内容检查一天:

SELECT EXISTS(SELECT id FROM hours WHERE business_id = 1 and dayofweek_id = 1)

如何在不针对每天运行单独查询的情况下检查每一天?

3 个答案:

答案 0 :(得分:2)

试试这个:

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;

两个选择将导致

+------------+------+---------------+
| 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)

+------+------------+---------------+
| 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)

答案 1 :(得分:1)

您可以使用SQL IN operator

SELECT 
EXISTS(
    SELECT id FROM hours WHERE business_id = 1 and dayofweek_id IN [1,2,3..etc]
)

假设您只是想确保一周中有一个小时它们是开放的。

答案 2 :(得分:0)

这样的事情:

select hours.*, dayofweek.name AS day
from dayofweek
inner join hours on hours.dayofweek_id = dayofweek.id
where hours.business_id = 1