title: MySQL: Group by two columns and sum
body: <p>Building an inventory system. I have lots of products and each product has three different variables. So for stock totals I want to group by the two columns (product &amp; size) and sum quantity to get stock total.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>product</th>
  <th>Size</th>
  <th>Quantity</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Widget one</td>
  <td>2</td>
  <td>275</td>
  </tr>
  <tr>
  <td>Widget one</td>
  <td>2</td>
  <td>100</td>
  </tr>
  <tr>
  <td>Widget two</td>
  <td>3</td>
  <td>150</td>
  </tr>
  <tr>
  <td>Widget two</td>
  <td>2</td>
  <td>150</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>What I want for output:<br /></p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>product</th>
  <th>Size</th>
  <th>Quantity</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Widget one</td>
  <td>2</td>
  <td>375</td>
  </tr>
  <tr>
  <td>Widget two</td>
  <td>3</td>
  <td>150</td>
  </tr>
  <tr>
  <td>Widget two</td>
  <td>2</td>
  <td>150</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I figured out how to group by one column and sum using the code below:</p>
  <pre><code>$query = &quot;SELECT product, SUM(Quantity) FROM inventory GROUP BY product&quot;;  
  $result = mysql_query($query) or die(mysql_error());
  // Print out result
  while($row = mysql_fetch_array($result)){
      echo &quot;Total &quot;. $row['product']. &quot; = &quot;. $row['SUM(Quantity)'];
      echo &quot;&lt;br /&gt;&quot;;
  }
  ?&gt;
  </code></pre>
  <p>I am just stuck on grouping by both columns. Is it possible? or should I just create three different products for the of the three sizes and eliminate that column? Thanks.</p>