Victor Aurélio
Victor Aurélio

Reputation: 2565

Get parent with more than 2 child

Table structure:

| id | parent |
   1     0
   2     1
   3     1
   4     2
   5     2
   6     2

How can I get the id of the parent row that have more than 2 childs ? (in the above case should return only the id=2 row)

Upvotes: 0

Views: 2192

Answers (2)

Pரதீப்
Pரதீப்

Reputation: 93734

I believe this is enough to find the parent id

SELECT parent
FROM   tablename
GROUP  BY parent
HAVING Count(parent) > 2 

Upvotes: 1

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

Try this query

SELECT *
FROM tablename 
WHERE id IN
(
    SELECT parent
    FROM tablename
    GROUP BY parent
    HAVING COUNT(parent) > 2
)

Upvotes: 2

Related Questions