title: MySQL select parent followed by its children and ordered by
body: <p>I have one level hierarchy table structure using Adjacency List Model. It could be summarized as follows:</p>
  
  <pre><code>id    parent_id      title          created_at
  
  1     null           name1          2017-05-30 08:05:00
  2     null           name2          2017-05-30 08:15:00
  3     1              another name   2017-05-30 09:00:00
  4     1              new name       2017-05-30 08:06:00
  5     2              new title      2017-05-30 08:18:04
  6     null           lorem          2017-05-30 08:04:00   
  </code></pre>
  
  <p>What I need to get is an sql query that returns every row of null <code>parent_id</code> i.e the parent followed by its children ordered by <code>created_at</code> Something like the following:</p>
  
  <pre><code>id    parent_id      title          created_at
  
  6     null           lorem          2017-05-30 08:04:00 
  1     null           name1          2017-05-30 08:05:00
  4     1              new name       2017-05-30 08:06:00
  3     1              another name   2017-05-30 09:00:00
  2     null           name2          2017-05-30 08:15:00
  5     2              new title      2017-05-30 08:18:04
  </code></pre>
  
  <p>I have tried </p>
  
  <pre><code>SELECT COALESCE(`parent_id`, `id`) as pid, title, created_at 
  FROM `items` 
  ORDER BY created_at
  </code></pre>
  
  <p>But it does not succeeded the <code>another name</code> record came separately at the end of the result set. </p>
  
  <p>This is <a href="http://sqlfiddle.com/#!9/d3dd87/3/0" rel="nofollow noreferrer">sql fiddle for the case</a></p>
  
  <p><strong>Notice</strong> In the real case the id is an UUID string.</p>