title: php + mysql two table sorting
body: <pre><code>+------------------------------------+
   | uid       | user_name   | food   |
   +-----------------------------------+
   | 12345     | Joe         | 3,1,2   |
   | 12346     | John        | 2,1     |
   | 12347     | David       | 30,2,1  |
   | 12355     | Peter       | 50,1    |
   +-----------------------------------+
  
  
  +-------------------------+
   | food_id  | food_name   |
   +------------------------+
   | 1        | apple       |
   | 2        | orange      |
   | 3        | banana      |
   | 50       | pear        |
   +------------------------+
  
  $user_sql = "SELECT `uid`,`user_name`,`food` FROM `user`";
  $stmt = $pdo-&gt;prepare($user_sql);
  $stmt-&gt;execute();
  $user_data = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
  $food_id = $user_data["food"]; // will be get eg: 50,1,3
  $food_sql = "SELECT * FROM `food` WHERE `food_id` in ($food_id)";
  $stmt = $pdo-&gt;prepare($food_sql);
  $stmt-&gt;execute();
  $food_data = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  </code></pre>
  
  <p>I will be foreach $food_data.</p>
  
  <p>That's OK, can show it.</p>
  
  <p>But this array($food_data) is sorting order by <code>food</code>.<code>food_id</code>, I need sort same to <code>user</code>.<code>food</code></p>
  
  <p><strong>EG show:</strong> </p>
  
  <p>Joe   - apple,orange,banana   (default sort by food_id 1,2,3)</p>
  
  <p>John  - apple,orange  (default sort by food_id 1,2)</p>
  
  <p>David - apple,orange,pear  (default sort by food_id 1,2,50)</p>
  
  <p>Peter - apple,pear  (default sort by food_id 1,50)</p>
  
  <p>but I need change it.</p>
  
  <p><strong>How to change to</strong> </p>
  
  <p>Joe   - banana,apple,orange  (sort same to <code>user</code>.<code>food</code> field value 3,1,2)</p>
  
  <p>John  - orange,apple  (sort same to <code>user</code>.<code>food</code> field value 2,1)</p>
  
  <p>David - pear,orange,apple  (sort same to <code>user</code>.<code>food</code> field value 30,2,1)</p>
  
  <p>Peter - pear,apple   (sort same to <code>user</code>.<code>food</code> field value 50,1)</p>
  
  <p><strong>I had try one by one select sql through the smarty template plugin.</strong></p>
  
  <p>But that's so wasted resources. I can do something in php or mysql?</p>
  
  <hr>