无法解决这个sql语句来归档我的真正目标

时间:2013-05-19 19:14:44

标签: mysql sql

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可以查询曼谷的每家酒店,每家酒店的费率最低

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

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

什么错,我该怎么做才能存档

请建议

提前感谢。

2 个答案:

答案 0 :(得分:3)

这可以大大简化 - 根据您需要的结果不需要子查询:

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

请注意,对于其他DBMS,您需要在GROUP BY子句中指定每个非聚合字段。 MySQL可以让你在没有它的情况下逃脱 - 无论好坏。

答案 1 :(得分:-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