Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with a mysql query

Tags:

mysql

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

like image 407
André Alçada Padez Avatar asked Dec 21 '22 12:12

André Alçada Padez


2 Answers

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
like image 175
The Scrum Meister Avatar answered Jan 07 '23 17:01

The Scrum Meister


SELECT userid FROM (
    SELECT userid, count(*) as cnt FROM some_table GROUP BY userid
) WHERE cnt = 24
like image 24
Gerrat Avatar answered Jan 07 '23 18:01

Gerrat