我正在尝试创建一个产生的查询:
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
我究竟做错了什么?