创建一个处理包含多个事件的日历的MySQL查询

时间:2012-10-07 12:23:07

标签: mysql date

我正在尝试创建一个Produces:

的查询
VENUE | 2012-10-01 | 2012-10-02 | 2012-10-03
 01   |  OCCUPIED  |   EMPTY    |  OCCUPIED
 02   |   EMPTY    |   EMPTY    |  OCCUPIED
 03   |  OCCUPIED  |   EMPTY    |   EMPTY

来自这两个表:

地点表:

venue_id | venue
   1     |   01
   2     |   02
   3     |   03

活动表:

event_id |    start   |    end     | venue_id
   1     | 2012-10-01 | 2012-10-02 |    1
   2     | 2012-10-03 | 2012-10-04 |    1
   3     | 2012-10-03 | 2012-10-04 |    2
   4     | 2012-10-01 | 2012-10-02 |    3

有人能提出解决这个问题的最佳方法吗?

我目前的做法是:

SELECT 
   venue,
   IF(start <= '2012-10-01' AND '2012-10-01' < end, 'OCCUPIED','EMPTY') AS '2012-10-01',
   IF(start <= '2012-10-02' AND '2012-10-02' < end, 'OCCUPIED','EMPTY') AS '2012-10-02',
   IF(start <= '2012-10-03' AND '2012-10-03' < end, 'OCCUPIED','EMPTY') AS '2012-10-03'
FROM Venue as v, Event as e
WHERE e.venue_id = v.venue_id
GROUP BY v.venue

然而,我没有得到理想的结果......我得到了以下内容:

VENUE | 2012-10-01 | 2012-10-02 | 2012-10-03
 01   |  OCCUPIED  |   EMPTY    |   EMPTY
 02   |   EMPTY    |   EMPTY    |  OCCUPIED
 03   |  OCCUPIED  |   EMPTY    |   EMPTY

我做错了什么?

1 个答案:

答案 0 :(得分:2)

奇怪的是,你的select语句甚至没有返回你发布的结果集中命名的列。此外,我注意到您正在重复查询“2012-10-02”。

SELECT 
   IF(start <= '2012-10-01' AND '2012-10-01' < end, 'OCCUPIED','EMPTY') AS '2012-10-01',
   IF(start <= '2012-10-02' AND '2012-10-02' < end, 'OCCUPIED','EMPTY') AS '2012-10-02',
   IF(start <= '2012-10-03' AND '2012-10-03' < end, 'OCCUPIED','EMPTY') AS '2012-10-03'
FROM Venue as v, Event as e
WHERE e.venue_id = v.venue_id
GROUP BY v.venue

修改

以下两个活动共享相同的venue_id和场地,您的查询按场地分组,这样就可以在2012-10-03'日压制OCCUPIED结果。

event_id |    start   |    end     | venue_id
   1     | 2012-10-01 | 2012-10-02 |    1
   2     | 2012-10-03 | 2012-10-04 |    1

SQL FIDDLE:http://sqlfiddle.com/#!2/dc33f/20

你可以尝试添加像MAX这样的聚合函数来获取记录子集中包含的任何OCCUPIED事件:

SELECT e.*, v.venue,
   max(IF(start <= '2012-10-01' AND '2012-10-01' < end, 'OCCUPIED','EMPTY')) AS '2012-10-01',
   max(IF(start <= '2012-10-02' AND '2012-10-02' < end, 'OCCUPIED','EMPTY')) AS '2012-10-02',
   max(IF(start <= '2012-10-03' AND '2012-10-03' < end, 'OCCUPIED','EMPTY')) AS '2012-10-03'
  from event e,
       venue v
where e.venue_id = v.venue_id
GROUP BY v.venue

SQL FIDDLE:http://sqlfiddle.com/#!2/dc33f/22