title: Mysql Query to select multi-level children from table
body: <p>This is my MySQL table layout of user table.<br>
  Is it possible to  </p>
  
  <ol>
  <li>Get the count of children under each person. </li>
  <li>To select the person with at least 2 children under him. </li>
  </ol>
  
  <blockquote>
  <pre><code>+----+------+--------+
  | id | Name | Parent |
  +----+------+--------+
  | 1  |    A |      0 |
  +----+------+--------+
  | 2  |    B |      0 |
  +----+------+--------+
  | 3  |    C |      1 |
  +----+------+--------+
  | 4  |    D |      3 |
  +----+------+--------+
  | 5  |    E |      2 |
  +----+------+--------+
  </code></pre>
  </blockquote>
  
  <p><strong>The expected answer are</strong><br>
   1. </p>
  
  <blockquote>
  <pre><code>+----+------+----------+
  | id | Name | Children |
  +----+------+----------+
  | 1  |    A |  2(C, D) |
  +----+------+----------+
  | 2  |    B |     1(E) |
  +----+------+----------+
  | 3  |    C |     1(D) |
  +----+------+----------+
  | 4  |    D |        0 |
  +----+------+----------+
  | 5  |    E |        0 |
  +----+------+----------+
  </code></pre>
  </blockquote>
  
  <p>2.</p>
  
  <blockquote>
  <pre><code>+----+------+----------+
  | id | Name | Children |
  +----+------+----------+
  | 1  |    A |  2(C, D) |
  +----+------+----------+
  </code></pre>
  </blockquote>