title: Order by subgroup
body: <p>I need help building a MySQL query that will output all rows in a certain order.  The table definition contains <code>id</code>, <code>sub_id</code>, and <code>name</code>.  The <code>sub_id</code> column is used to indicate that a given row should be grouped within <code>id</code>, and if <code>sub_id</code> is blank/0/null, then the row is a top-level row.</p>
  
  <p>The desired sort order is by <code>name</code>, except I want all <code>sub_id</code> rows to be grouped within their corresponding <code>id</code> row. Within each subgroup, each row should be sorted by name.</p>
  
  <p>For example, given this unordered table:</p>
  
  <pre><code>id   sub_id   name
  =====================
  1    0        bananas
  2    0        apples
  3    0        apricots
  4    2        strawberries
  5    2        cherries
  6    1        oranges
  </code></pre>
  
  <p>The desired order is:</p>
  
  <pre><code>id   sub_id   name
  =====================
  2    0        apples
  5    2        cherries
  4    2        strawberries
  
  3    0        apricots
  
  1    0        bananas
  6    1        oranges
  </code></pre>
  
  <p>(spacing between subgroups for illustrative purposes only)</p>
  
  <p>Help?  Thanks in advance!</p>