简体   繁体   中英

Group by pick row with lowest value (price)

在此处输入图片说明

I have all the hotel and room price, in these data, I need to fetch the lowest price room among the hotels data, below is my query it is not picking the least price example hotelname3, room2, 5.

SELECT *
FROM (hoteldata)
GROUP BY hotelname
ORDER BY price ASC

在此处输入图片说明

尝试这个:

SELECT * FROM hoteldata where price = (SELECT min(price) FROM hoteldata)
SELECT hd.*
FROM (
    SELECT hotelname , MIN(price) as minprice
    FROM hoteldata
    GROUP BY hotelname
) minprices
JOIN hoteldata hd 
    ON  hd.hotelname = minprices.hotelname
    AND hd.price = minprices.minprice

Note: If two rooms in one hotel have the lowest price, they will be listet both.

For performance you should define an index on ( hotelname , price )

http://sqlfiddle.com/#!9/10f00/1

SELECT *
FROM (hoteldata) order by price asc limit 1;

This will work for getting minimum price room from different hotel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM