title: SQL: Ordering a UNION selection and displaying a ('string + count') if null
body: <p>I have quite a complex query here (for me at least):</p>
  
  <pre><code>SELECT h.name,  IFNULL(tip.type, 'positive') type,
  CONCAT('Total ', tip.type, 's available = ', COUNT(tip.type)) total
  
  FROM racing.race r, racing.horse h 
      LEFT JOIN racing.horse_race_details hrd ON hrd.horse_id = h.horse_id 
      LEFT JOIN racing.proform_tip tip ON h.horse_id = tip.horse_id AND tip.race_id = hrd.race_id
  WHERE r.race_id = hrd.race_id AND r.race_id = 123456
  GROUP BY h.name, tip.type
  
  UNION 
  
  SELECT h.name, IFNULL(tip.type, 'negative') type,
  CONCAT('Total ', tip.type, 's available = ', COUNT(tip.type)) total
  
  FROM racing.race r, racing.horse h 
      LEFT JOIN racing.horse_race_details hrd ON hrd.horse_id = h.horse_id 
      LEFT JOIN racing.proform_tip tip ON h.horse_id = tip.horse_id AND tip.race_id = hrd.race_id
  WHERE r.race_id = hrd.race_id AND r.race_id = 123456
  GROUP BY h.name, tip.type ;
  </code></pre>
  
  <p>Which creates a selection like this: </p>
  
  <p><img src="https://i.stack.imgur.com/kvBcV.png" alt="current query result"></p>
  
  <p>However, I need to do 2 more things with this:</p>
  
  <ol>
  <li>order this by h.name (just adding ORDER BY causes it to break)</li>
  <li>display the string 'Total negatives available = 0' if it's NULL</li>
  </ol>