显示所有房间和预订

时间:2014-08-18 07:55:29

标签: mysql join

我有三张桌子

  • 类型(id,libel)

  • 卧室(身份证,姓名,类型)

  • 预订(id,name,libel,start_date,end_date,room)

我想制作一个应用程序,您可以在两个日期之间获得所有房间和预订。我有以下查询

$sql = 'SELECT r.id, r.name, r.start_date, r.end_date, r.libel, r.statut ,c.name as room, t.libel as type '
            . 'FROM type t INNER JOIN Bedroom c ON t.id = c.type LEFT JOIN reservation r ON c.id = r.room'
            . 'WHERE ( :start_date BETWEEN r.start_date AND r.end_date '
            . ' OR :end_date BETWEEN r.start_date AND r.end_date'
            . ' OR r.start_date BETWEEN :start_date AND :end_date'
            . ' OR r.end_date BETWEEN :start_date AND :end_date) ORDER BY c.'.$orderColumn.' ASC';

但似乎在start_date和end_date之间保留了房间。

2 个答案:

答案 0 :(得分:0)

$sql = 'SELECT Reservation.*, Bedroom.*, Type.* FROM Reservation '
. 'RIGHT JOIN Bedroom ON Reservation.room = Bedroom.id '
. 'INNER JOIN Type ON Bedroom.Type = Type.id'
. 'WHERE $someDate BETWEEN $startDate AND $endDate'
. 'ORDER BY $orderColumn ASC;'

这有用吗?顺便说一句,您未在AS中使用SELECT条款。您的:start_date:end_date指的是什么?

答案 1 :(得分:0)

解决方案是将WHERE更改为AND

$sql = 'SELECT r.id, r.name, r.start_date, r.end_date, r.libel, r.statut ,c.name as room, t.libel as type '
        . 'FROM type t INNER JOIN Bedroom c ON t.id = c.type LEFT JOIN reservation r ON c.id = r.room'
        . 'AND( :start_date BETWEEN r.start_date AND r.end_date '
        . ' OR :end_date BETWEEN r.start_date AND r.end_date'
        . ' OR r.start_date BETWEEN :start_date AND :end_date'
        . ' OR r.end_date BETWEEN :start_date AND :end_date) ORDER BY c.'.$orderColumn.' ASC';