title: Select distinct values from query excluding sorting-purpose column
body: <h3>Description:</h3>
  
  <p>I want to select my site content's categories. Most of them will be created by users so I will ve to deal with problem of many categories in table. I want to respect some kind content's trends on my site. My solution is:</p>
  
  <ul>
  <li>Select all categories from past 2 days and sort it by number of appearances (ascending),</li>
  <li>Union query (distinct)</li>
  <li>Select all categories from date &lt; past 2 days and sort it like above.</li>
  </ul>
  
  <p>Thanks to it I ve all most popular categories from small amount of time + most popular categories in global scope.</p>
  
  <h3>Query:</h3>
  
  <pre><code>(SELECT category, COUNT(*) AS number FROM data WHERE date BETWEEN ADDDATE(NOW(), INTERVAL -2 DAY) AND NOW() GROUP BY category)
  UNION  
  (SELECT category, COUNT(*) AS number FROM data WHERE date &lt; ADDDATE(NOW(), INTERVAL -2 DAY) GROUP BY category)
  ORDER BY number DESC LIMIT 50
  </code></pre>
  
  <h3>Output:</h3>
  
  <pre><code>+----------+--------+
  | category | number |
  +----------+--------+
  |        2 |      3 |
  |        4 |      3 |
  |        6 |      3 |
  |        5 |      2 |
  |        1 |      2 |
  |        2 |      1 |
  +----------+--------+
  6 rows in set (0.00 sec)
  </code></pre>
  
  <p>Note there is duplicated content in category (id 2), UNION DISTINCT (default) is not excluding this because it compares rows from both columns, so:</p>
  
  <pre><code>+----------+--------+
  | category | number |
  +----------+--------+
  |        2 |      3 | //is not equal to
  |        2 |      1 | //below values
  +----------+--------+
  //wont be excluded
  </code></pre>
  
  <h3>Problem to slove:</h3>
  
  <p>I need to select <strong>distinct values</strong> from only <strong>category column</strong>. </p>
  
  <p><em>(<strong>number</strong> is only for sorting purposes and used only in this query)</em></p>