假设我有下表:
#hotel_id #room_id #price
3 4 50.00
3 5 45.00
3 6 50.00
4 5 45.00
4 7 50.00
4 8 45.00
5 4 45.00
5 8 50.00
5 9 45.00
我必须执行什么查询才能获得唯一 #hotel_id 的数量 - 在这种情况下,它是 3,4,5 = 3 个唯一 ID 可能听起来很简单,但是...... :) 提前谢谢你
########### 我的查询select
Count(Distinct a.id),
rta.room_type_id,
IF(SUM(price = 0), 0, SUM(price))
from
adverts a,
rooms_to_adverts rta,
room_types rt,
prices_adverts pa
where
a.town = 'Sofia' and a.id = pa.advert_id
and rta.room_type_id = rt.id
and pa.room_type_id = rta.room_type_id
and rta.advert_id = a.id
and (pa.date >= '2013-09-20'
AND pa.date < '2013-09-23')
and rt.occupants >= '1'
GROUP BY a.id , rta.room_type_id
order by a.id , rt.occupants ASC
这个查询输出这个
看起来不是那么简单:)