帮助mysql查询

时间:2011-06-20 20:24:20

标签: mysql

对于这个问题的特殊性我很抱歉,但我已经48小时了,现在我的思绪可能已经无效了。

所以,我有一张桌子 - > ID |用户ID |卡
用户可以拥有多张卡

我只需要查询该表以查找具有24个不同卡的用户ID(有重复卡的用户)。

有人能帮助我吗?

谢谢

3 个答案:

答案 0 :(得分:10)

SELECT UserID
FROM tableName
GROUP BY UserID
HAVING COUNT(*) = 24

请参阅GROUP BY (Aggregate) Functions

更新以查找包含24张不同卡片的用户:

SELECT UserID
FROM (
    SELECT UserID
    FROM tableName
    GROUP BY UserID, Card
) rs
GROUP BY UserID
HAVING COUNT(*) = 24

可替换地:

SELECT UserID
FROM tableName
GROUP BY UserID
HAVING COUNT(DISTINCT Card) = 24

答案 1 :(得分:1)

SELECT userid FROM (
    SELECT userid, count(*) as cnt FROM some_table GROUP BY userid
) WHERE cnt = 24

答案 2 :(得分:0)

select * from 
    ( select count(*) cnt,card from tablename group by card ) 
where cnt=24