Tom Smedley
Tom Smedley

Reputation: 49

Add additional condition to MySQL query

I need some help with a MySql Query. My existing query returns all rows which have 100001 as the query_id, sorted by priceperadult ASC.

However, I want it to only return the best rate per hotel, so need to add to the statement so only one rate per hotel shows, whats the best way to achieve this?

SELECT * FROM `search_results` WHERE `query_id` = '100001' ORDER BY `priceperadult` ASC

Sample Data

query_id, priceperadult, hotel_name

100001, 100, Hotel 1

100001, 200, Hotel 1

100001, 100, Hotel 2

100001, 200, Hotel 2

Upvotes: 0

Views: 26

Answers (1)

sgeddes
sgeddes

Reputation: 62831

Sounds like you're looking to use min():

SELECT query_id, MIN(priceperadult), hotel_name
FROM search_results 
WHERE query_id = '100001' 
GROUP BY query_id, hotel_name

Upvotes: 1

Related Questions