2011-09-30 23 views
-1

Appartments公寓表:获取可用的个公寓

appartments table http://f.cl.ly/items/1u0j2o1w3H0h0w2H3f2G/Screen%20Shot%202011-09-30%20at%2010.53.13.png

booking: http://f.cl.ly/items/2T012f351J3S3y3a421d/Screen%20Shot%202011-09-30%20at%2010.55.55.png

我想人未预订的可用个公寓。 这是我目前的MySQL查询:

(array) $db->getRecords(' 
SELECT i.id, i.num_persons, i.rating, i.lat, i.lng, i.street, i.number, 
    c.title, c.introduction, c.text, c.area, c.long_term_rental, c.beds, c.features, 
    c.services_and_equipment, c.terms_and_conditions, m.url 
FROM appartments AS i 
INNER JOIN appartments_content AS c on c.parent_id = i.id 
INNER JOIN meta AS m on m.id = c.meta_id 
LEFT JOIN appartments_bookings AS b on b.appartment_id = i.id 
WHERE c.language = ? 
    AND i.hidden = ? 
    AND i.publish_on <= ? 
    AND i.city_id = ? 
    AND i.num_persons >= ? 
    AND (b.arrival >= ? OR b.departure <= ?) 
ORDER BY i.num_persons ASC, i.publish_on DESC 
LIMIT ?, ?', 

array(FRONTEND_LANGUAGE, 'N', FrontendModel::getUTCDate('Y-m-d H:i') . ':00', 
    (int) $filter['city'], (int) $filter['persons'], $filter['arrival'], 
    $filter['departure'], (int) $offset, (int) $limit), 'id'); 
+2

你的问题是什么?怎么样的查询不起作用? –

+0

即使他们被预订,我仍然可以获得所有的公寓。 –

+0

DB中的值是什么? –

回答

2
SELECT a.id, a.num_persons, a.rating, a.lat, a.lng, a.street, a.number 
    ,c.title, c.introduction, c.text, c.area, c.long_term_rental, c.beds 
    , c.features ,c.services_and_equipment, c.terms_and_conditions 
    , m.url 
FROM appartments AS a 
INNER JOIN appartments_content AS c on c.parent_id = a.id 
INNER JOIN meta AS m on m.id = c.meta_id 
LEFT JOIN appartments_bookings AS b 
     ON (b.appartment_id = a.id 
     AND NOT ((? > b.departure) OR (? < b.arrival)))  
    -- this ? = arrival  that ? = departure 
WHERE b.id IS NULL 
    AND c.language = ? 
    AND a.hidden = ? 
    AND a.publish_on <= ? 
    AND a.city_id = ? 
    AND a.num_persons >= ? 

ORDER BY a.num_persons ASC, a.publish_on DESC 
LIMIT ? OFFSET ?' 

变化

职业测试
测试NOT((param_arrival > b.departure) OR (param_departure < b.arrival))是一个简单的测试重叠,与2周的测试工作。你的并不是因为它总共需要4次测试,所以你错过了一些东西。
当然这个代码假设到达始终在出发前,所以确保你断言这一假设。

使用反连接上预订
我们把所有的测试为职业的连接标准的预订。
然后我只有选择的行与没有预订,即b.id IS NULL。

LIMIT子句
这只是我的一个宠物忿怒,但我更喜欢更加直观LIMIT rowcount OFFSET pagestart语法在混乱​​。

+0

使敏感,它现在的作品。 –

+0

我有一个后续问题。 我需要在表格中检查apartment_booking(单个)是否状态未取消,取消了公寓应显示在结果中。 我编辑查询到这个:http://pastie.org/2711211(我用pastie因为字符输入似乎是有限的) 但现在我没有结果。 有什么想法? –