php - Counting number of children -


having 2 mysql table looking this:

table parents

+-------+-----------+-----------+ | id    |  name     | birthdate | +-------+-----------+-----------+ |     1 | mary      | 1974-05-02| |     2 | john      | 1970-06-03| |     4 | james     | 1984-07-04| 

table children

+-------+-----------+-----------+-----------+-----------+ | id    |  parent   | name      |birthdate  |  gender   | +-------+-----------+-----------+-----------+-----------+ |     1 | 1         | sara      |2013-10-22 | female    | |     2 | 1         | jack      |2014-05-02 | male      | |     3 | 1         | jill      |2015-06-07 | female    | |     4 | 2         | sam       |2015-06-07 | male      | |     5 | 2         | fred      |2015-06-07 | male      | |     6 | 3         | julie     |2015-06-07 | female    | |     7 | 4         | megan     |2015-06-07 | female    | 

how retrieve number of parents (count) have 3 children?

+---------+ |total    | +---------+ | 1       | 

use count , having:

select p.id parents p inner join children c     on c.parent = p.id group p.id having count(c.id) = 3 

to number of parents 3 children:

select count(*) (     select p.id     parents p     inner join children c         on c.parent = p.id     group p.id     having count(c.id) = 3 ) t 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -