title: MYSQL Order By List
body: <p>I have table A which have an 'ID' and 'Message' and another table B which is ordered. I wish to order table A according to how they are ordered in table B.</p>
  
  <p>Example - Table A:</p>
  
  <pre><code>ID    Message
  -------------
  1     ABC
  2     DEF
  3     HIJ
  4     KLM
  5     NOP
  </code></pre>
  
  <p>Table B: </p>
  
  <pre><code>ID
  ---
  5
  2
  4
  1
  3
  </code></pre>
  
  <p>Expected Result:</p>
  
  <pre><code>ID    Message
  -------------    
  5     NOP
  2     DEF
  4     KLM
  1     ABC
  3     HIJ
  </code></pre>
  
  <p>I have the following query:</p>
  
  <pre><code>SELECT DISTINCT s.message, s.stream_id
  FROM Stream s
  ORDER BY ..... ;
  </code></pre>
  
  <p>How can I do it?
  Apparently using FIND_IN_SET doesn't take into consideration the order of Table B</p>
  
  <p>UPDATE</p>
  
  <p>Table B is getting ordered as follows:</p>
  
  <pre><code>SELECT stream_id 
  FROM SearchCache 
  GROUP BY stream_id 
  ORDER BY COUNT(stream_id);
  </code></pre>
  
  <p>Therefore, the query should be something as follows:</p>
  
  <pre><code>SELECT DISTINCT s.message, s.stream_id
  FROM Stream s
  ORDER BY s.stream_id *SOMETHING* (SELECT stream_id 
                                    FROM SearchCache 
                                    GROUP BY stream_id 
                                    ORDER BY COUNT(stream_id)) ASC;
  </code></pre>