title: Propper way of creating a mysql query
body: <p>What would be the correct way of building a sql query, in this scenario:</p>
  
  <pre><code>$data = $db-&gt;query("SELECT * FROM users");
  
  foreach ($data as $row) {
     $data2 = $db-&gt;one("SELECT amount FROM cart WHERE id = " . $row['id']);
     echo $row['name'] . '&lt;br&gt;';
     echo $data2['amount'] . '&lt;br&gt;';
  
  }
  </code></pre>
  
  <p>In the above example, if I have 500 users, would that equal to 500 database calls?
  Or should I use single query like:</p>
  
  <pre><code>  $data = $db-&gt;query("SELECT *" 
    . "\n (SELECT SUM(amount) FROM cart WHERE user.id = cart.user_id) as total" 
    . "\n FROM users";
  </code></pre>
  
  <p>Are there any advantages or disadvantages between the two?</p>