title: How to use correctly several count(*) and select in a single table?
body: <p>I'm working with a single MySQL table called <code>location</code>. My columns are:</p>
  
  <ul>
  <li><code>idLocation</code>: the location id.</li>
  <li><code>idParent</code>: the direct parent location id.</li>
  <li><code>checked</code>: a boolean indicating if the location has been checked or not.</li>
  </ul>
  
  <p>I'm calling level 1 location each location who has no parents / level 2 each location who has a level 1 parent / level 3 each location who has a level 2 parent.</p>
  
  <p>I'm trying to find all level 1 locations and to count their undirect children (i.e. all the level 3 locations who are undirectly related) and to count how many of them have been checked.</p>
  
  <p>For example, if a lvl.1 location has 4 lvl.3 children and 1 has been checked, I would like to get <code>1/4</code> as result.</p>
  
  <p>I've tried several queries, but I'm a beginner and so far I've only managed to obtain the number of children of each location (including the level 2 ones, but it's not what I'm looking for).</p>
  
  <p>There is a query in order to create a reproducible example:</p>
  
  <pre><code>CREATE TABLE location (idLocation int, idParent int, checked tinyint(1));
  INSERT INTO location (idLocation, idParent, checked)
  VALUES (1, null, 0), (2, 1, 0), (3, 1, 0), (4, 3, 1), (5, 3, 0), (6, 3, 0),
  (7, 3, 0), (8, 3, 0), (9, null, 0), (10, 9, 0), (11, 10, 0), (12, 10, 0);
  </code></pre>
  
  <p>In this situation, we should find has a result:</p>
  
  <ul>
  <li>idLocation 1 with 4 level 3 childrens. 1 of them is checked.</li>
  <li>idLocation 9 with 2 level 3 childrens. 1 of them is checked.</li>
  </ul>