嘿,你能帮我做简单的sql查询吗?
表
旅馆
id | name | country | rating
1 Bali bg 4.5
2 Beach uk 4.0
3 Blue sk 3.7
旅游
id | id_hotel | price
1 2 250
2 2 270
3 1 250
4 1 300
5 3 250
我想要这样的东西
SELECT t.* FROM tour t LEFT JOIN hotel h ON t.id_hotel = h.id
WHERE h.country = uk GROUP BY h.id ORDER BY t.price
所以我想每家酒店只得到一排,最低旅游价格。最好使用 Doctrine 2 queryBuilder。
感谢您的帮助。
嘿,大概我有解决方案
SELECT MIN(tour.id), hotel.* FROM `tour` INNER JOIN (
SELECT MIN(t.final_price) as final_price, t.id_hotel FROM tour t GROUP
BY t.id_hotel) ht
ON tour.id_hotel = ht.id_hotel AND tour.final_price =
ht.final_price
LEFT JOIN hotel ON hotel.id = tour.id_hotel GROUP BY tour.id_hotel
使用分组依据和排序依据来完成您的结果,请尝试以下查询
select hotel.name,Tour.id,Tour.price,Hotel.rating from Hotel inner join Tour on Tour.id_hotel = Hotel.id group where hotel.country = 'uk' group by Tour.id_hotel order by Tour.price asc
让我知道它是否有帮助
[编辑] 此查询应该适用于您