按顺序排队?

时间:2015-07-14 08:11:16

标签: mysql join

我有两个表,第一个是hotel_info,其中包含(hotel_idhotel_namelocation)和第二个表rooms等字段,有空间信息,例如(room_idroom_namehotel_idratedescription)。我想要一个查询,返回酒店的最低房价以及hotel_info表中有关酒店的所有信息。到目前为止,我有这个查询正常工作,但它没有返回最低房价。

SELECT a.hotel_id,a.hotel_name ,a.location, b.rate FROM hotel_info a 
LEFT JOIN rooms b ON b.hotel_id=a.hotel_id 
GROUP BY hotel_id

如何获得每间酒店的最低房价?

2 个答案:

答案 0 :(得分:2)

您可以使用mysql函数min(),例如

select a.hotel_id, min(b.rate) 
from hotel_info a 
left join rooms b on b.hotel_id = a.hotel_id 
group by a.hotel_id;

答案 1 :(得分:1)

SELECT a.hotel_id, a.hotel_name, a.location, min(b.rate) as min_rate 
FROM hotel_info a 
LEFT JOIN rooms b ON b.hotel_id = a.hotel_id 
GROUP BY a.hotel_id, a.hotel_name, a.location