I'm sorry for the specificity of this question, but i've been up for 48h and my mind is probably void right now.
so, I have a table -> id|userid|card
where a user can have multiple cards
I just need to query the table to find the userid that have exactly 24 distinct cards (there are users with repeated cards).
Can someone help me please?
Thank you
SELECT UserID
FROM tableName
GROUP BY UserID
HAVING COUNT(*) = 24
See GROUP BY (Aggregate) Functions
Update: to find users that have 24 distinct cards:
SELECT UserID
FROM (
SELECT UserID
FROM tableName
GROUP BY UserID, Card
) rs
GROUP BY UserID
HAVING COUNT(*) = 24
Alternatively:
SELECT UserID
FROM tableName
GROUP BY UserID
HAVING COUNT(DISTINCT Card) = 24
SELECT userid FROM (
SELECT userid, count(*) as cnt FROM some_table GROUP BY userid
) WHERE cnt = 24
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With