title: MySQL loop over table rows, run another query, join results
body: <p>I have a table <code>my_table</code> like (and the NAME column values are unique):</p>
  
  <pre><code>ID   NAME   RANK   AGE
  1    John   4      21
  2    Sam    1      43
  3    Tom    3      57
  4    Bob    2      39
  5    Dave   5      25
  </code></pre>
  
  <p>I want to run a single query where for every row I select all the attributes plus another column that is a list of other names sorted by age DESC and NOT itself with a LIMIT (let's say 3), with the results something like:</p>
  
  <pre><code>NAME   RANK   AGE   SOME_LIST
  John   4      21    Tom,Sam,Bob
  Sam    1      43    Tom,Bob,Dave
  Tom    3      57    Sam,Bob,Dave
  Bob    2      39    Tom,Sam,Dave
  Dave   5      25    Tom,Sam,Bob
  </code></pre>
  
  <p>So the query to just get <code>SOME_LIST</code> for John would be something like:</p>
  
  <pre><code>SELECT name FROM my_table WHERE name &lt;&gt; 'John' ORDER BY age DESC LIMIT 3;
  </code></pre>
  
  <p>and the result would look like:</p>
  
  <pre><code>NAME
  Tom
  Sam
  Bob
  </code></pre>
  
  <p>I'm not sure 1) how to get these results into a single column comma-separated and 2) how to run this query for every row in <code>my_table</code>.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Getting closer with this query:</p>
  
  <pre><code>SELECT *, (SELECT GROUP_CONCAT(name) FROM my_table t1 WHERE t1.name &lt;&gt; t2.name ORDER BY age DESC LIMIT 3) AS some_list FROM my_table t2;
  </code></pre>
  
  <p>But the <code>some_list</code> column does not respect the age desc order by and also does not respect the limit. Only the <code>&lt;&gt;</code> is accepted.</p>