title: SQL: How to keep rows order with DISTINCT?
body: <p>The following SQL query: </p>
  
  <pre><code>SELECT messages.id, messages.created_at, comments.created_at FROM messages
  LEFT JOIN comments ON comments.message_id = messages.id 
  WHERE (messages.id IN (429,443)) 
  ORDER BY GREATEST(messages.created_at, comments.created_at) DESC
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code> id         messages.created_at     comments.created_at
  --------------------------------------------------------
   443                2                       5
   429                1                       4
   443                2                       3
  
   (I replaced dates with numbers for readability)
  </code></pre>
  
  <p>To get each <code>id</code> only once I added <code>DISTINCT</code>:</p>
  
  <pre><code>SELECT DISTINCT messages.id FROM messages
  LEFT JOIN comments ON comments.message_id = messages.id 
  WHERE (messages.id IN (429,443)) 
  ORDER BY GREATEST(messages.created_at, comments.created_at) DESC
  </code></pre>
  
  <p>But, in the result the <code>id</code> values changed order:</p>
  
  <pre><code>id
  ---
  429
  443
  </code></pre>
  
  <p>What could be the reason for that ? </p>
  
  <p>How could I keep the order ?</p>