Mysql获取在数据库中重复的唯一ID的计数

时间:2013-09-20 08:53:54

标签: mysql unique rows counting

假设我有下表:

#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   

此查询输出此内容 enter image description here

看起来并非如此简单:)

1 个答案:

答案 0 :(得分:0)

你已经拥有的部分答案是:

SELECT
COUNT(DISTINCT adverts.id)
FROM adverts

或者你有没有告诉过我们的并发症?