0
SELECT hotel.hotel_id, hotel.hotel_name, hotel.hotel_address, hotel.hotel_pic, 
room_accommodation.room_full_price 
FROM hotel 
INNER JOIN room_accommodation ON (room_accommodation.hotel_id = hotel.hotel_id ) 
INNER JOIN ( 
    SELECT MIN( room_full_price ) AS minPrice 
    FROM room_accommodation INNER JOIN hotel ON ( 
    hotel.hotel_id =room_accommodation.hotel_id ) 
    WHERE hotel.hotel_address LIKE '%bangkok%' 
)room_accommodation ON room_accommodation.room_full_price = room_accommodation.minPrice

这条SQL可以查询每家酒店在曼谷房价最低的每家酒店

但问题是现在我想查询每家酒店的所有最低价格但是当我削减时

结果出来的条件只给了我所有酒店的最低价格

有什么问题,我应该怎么做才能存档

请指教

预先感谢。

4

2 回答 2

3

This can be greatly simplified - there's no need for a subquery based on the results you need:

SELECT hotel.hotel_id, 
       hotel.hotel_name, 
       hotel.hotel_address, 
       hotel.hotel_pic, 
       MIN(room_accommodation.room_full_price) as room_full_price 
FROM hotel 
INNER JOIN room_accommodation 
  ON room_accommodation.hotel_id = hotel.hotel_id
  AND hotel.hotel_address LIKE '%bangkok%'
GROUP BY hotel.hotel_id

Note that for other DBMS's you would need to specify each non-aggregate field in the GROUP BY clause. MySQL lets you get away without that - for better or worse.

于 2013-05-19T19:20:14.847 回答
-1

我想你给你的内部查询 [room_accommodation] 提供了错误的别名

您的查询显示您还有一个名为 room_accommodation 的表

SELECT hotel.hotel_id, hotel.hotel_name, hotel.hotel_address, hotel.hotel_pic, 
room_accommodation.room_full_price 
FROM hotel 
INNER JOIN ***room_accommodation*** ON (room_accommodation.hotel_id = hotel.hotel_id ) 
INNER JOIN ( 
    SELECT MIN( room_full_price ) AS minPrice 
    FROM room_accommodation INNER JOIN hotel ON ( 
    hotel.hotel_id =room_accommodation.hotel_id ) 
    WHERE hotel.hotel_address LIKE '%bangkok%' 
)***room_accommodation*** ON room_accommodation.room_full_price = room_accommodation.minPrice
于 2013-05-19T19:21:48.690 回答