2010-03-18 57 views
1

我正在查询数据库以查找以下内容。MySQL - 查询出了什么问题?

如果客户在日期A和B之间搜索城市中的酒店,则在两个日期之间查找并返回房间空闲的酒店。

每种房型都会有不止一个房间(即5个房间为A型,10个房间为B型等),我们必须查询数据库才能找到那些至少有一个房间至少有一种房型免费。

这是我的表结构:

**Structure for table 'reservations'** 
    reservation_id 
    hotel_id 
    room_id 
    customer_id 
    payment_id 
    no_of_rooms 
    check_in_date 
    check_out_date 
    reservation_date 

    **Structure for table 'hotels'** 
    hotel_id 
    hotel_name 
    hotel_description 
    hotel_address 
    hotel_location 
    hotel_country 
    hotel_city 
    hotel_type 
    hotel_stars 
    hotel_image 
    hotel_deleted 


    **Structure for table 'rooms'** 
    room_id 
    hotel_id 
    room_name 
    max_persons 
    total_rooms 
    room_price 
    room_image 
    agent_commision 
    room_facilities 
    service_tax 
    vat 
    city_tax 
    room_description 
    room_deleted 

这是我的查询:

$city_search = '15'; 
$check_in_date = '29-03-2010'; 
$check_out_date = '31-03-2010'; 

$dateFormat_check_in = "DATE_FORMAT('$reservations.check_in_date','%d-%m-%Y')"; 
$dateFormat_check_out = "DATE_FORMAT('$reservations.check_out_date','%d-%m-%Y')"; 

$dateCheck = "$dateFormat_check_in >= '$check_in_date' AND $dateFormat_check_out <= '$check_out_date'"; 

$query = "SELECT $rooms.room_id, 
        $rooms.room_name, 
        $rooms.max_persons, 
        $rooms.room_price, 
        $hotels.hotel_id, 
        $hotels.hotel_name, 
        $hotels.hotel_stars, 
        $hotels.hotel_type 
      FROM $hotels,$rooms,$reservations 
      WHERE $hotels.hotel_city = '$city_search' 
      AND $hotels.hotel_id = $rooms.hotel_id 
      AND $hotels.hotel_deleted = '0' 
      AND $rooms.room_deleted = '0' 
      AND $rooms.total_rooms - (SELECT SUM($reservations.no_of_rooms) as tot 
                FROM $reservations 
                WHERE $dateCheck 
                GROUP BY $reservations.room_id) > '0'"; 

客房每家酒店的每个客房已经预订的数量将被存储在保留表。

事情是查询不会返回任何结果。即使它应该如果我手动计算它。

我试着单独运行子查询,我没有得到任何结果。而且我已经失去了相当数量的头发,试图从昨天开始解决这个查询。这有什么问题?还是有更好的方法来做我上面提到的?

编辑:代码编辑删除错误。感谢Mark Byers

Sample Data in reservation table 

1 1 1 2 1 3 2010-03-29 2010-03-31 2010-03-17 
2 1 2 3 3 8 2010-03-29 2010-03-31 2010-03-18 
5 1 1 5 5 4 2010-03-29 2010-03-31 2010-03-12 

子查询应返回

Room ID : 1 Rooms Booked : 7 
Room ID : 2 Rooms Booked : 8 

But it does not return any value at all.... If i remove the dateCheck condition it returns 
Room ID : 2 Rooms Booked : 8 

回答

1

你的问题是在这里:

$rooms.total_rooms - (SELECT SUM($reservations.no_of_rooms) as tot, 
               $rooms.room_id as id 
               FROM $reservations,$rooms 
               WHERE $dateCheck 
               GROUP BY $reservations.room_id) > '0'" 

你是做减法total_rooms - (tot, id)其中第一个操作数是标量值,第二个是一张桌子有两列。删除结果集中的一列,并确保只返回一行。

您还应该使用JOIN关键字进行连接,而不是用逗号分隔表格。这样你就不会忘记添加连接条件。

你可能想沿着这些路线的东西:

SELECT column1, column2, etc... 
FROM $hotels 
JOIN $rooms 
ON  $hotels.hotel_id = $rooms.hotel_id 
JOIN (
    SELECT SUM($reservations.no_of_rooms) as tot, 
      $rooms.room_id as id 
      FROM $reservations 
      JOIN $rooms 
      ON ??? /* Aren't you missing something here? */ 
      WHERE $dateCheck 
      GROUP BY $reservations.room_id 
) AS T1 
ON  T1.id = room_id 
WHERE $hotels.hotel_city = '$city_search' 
AND $hotels.hotel_deleted = '0' 
AND $rooms.room_deleted = '0' 
AND $rooms.total_rooms - T1.tot > '0' 
+0

谢谢...的错误消失......但查询本身没有返回任何结果..即使它应该......我不能”唔知道我的生活...我想知道是否有人可以指出我逻辑上出错的地方.. – SpikETidE 2010-03-18 07:06:17

+0

我怀疑你的子查询仍然是错误的。 GROUP BY看起来很可疑,你可能是指与WHERE相关的子查询。加入子查询可能会更好,而不是使用相关的子查询。然后你确实想要返回这个id。 – 2010-03-18 07:10:12

+1

为什么你在查询中的房间和预订之间没有明显的关系?我强烈建议您使用新的ANSI“JOIN ... ON ...”语法重写此查询。 – 2010-03-18 07:11:37