Select room with minimal price on city
开发者_JAVA技巧I have 3 table Room, Hotel, City, how can I select one room with minimal price and its hotel_id
for each city
price | hotel_id | city_id
10 | 1 | 1
13 | 4 | 2
44 | 7 | 3
...
SELECT MIN(price) price, hotel_id, city_id FROM (
SELECT MIN(room_price1) price, hotel_id, city_id
FROM Hotel
JOIN Room USING(hotel_id)
JOIN City USING(city_id)
LEFT JOIN Service_hotel_option USING (hotel_id)
WHERE room_price1 > 0
GROUP BY hotel_id
ORDER BY price ASC) t
GROUP BY city_id
ORDER by city_name, price
but I am not sure.
How about:
SELECT price, hotel_id, city_id
FROM hotel
INNER JOIN room AS r1 USING (hotel_id)
WHERE NOT EXISTS (SELECT 1 FROM room AS r2
WHERE r2.hotel_id = r1.hotel_id AND r2.price < r1.price)
This will only select rows from hotel INNER JOIN room
if there are no rooms to be found that have lower prices.
精彩评论