title: Modify MySQL Query or Run in PHP
body: <p>I'm trying to run a query to find which inventory I should promote and which campaign I should run so I can move that inventory.</p>
  
  <p>I have three tables:</p>
  
  <ul>
  <li><code>campaigns</code> lists different campaigns that I can run, each campaign has a unique id. Some campaigns promote only one item and some promote multiple items.</li>
  <li><code>inventory</code> has all the items I have in stock and the quantity of those items.</li>
  <li><code>campaign_to_inventory</code> matches the unique campaign id to the inventory item.</li>
  </ul>
  
  <p><code>campaigns</code>:</p>
  
  <pre><code>name         | id
  -------------|---
  blue-widgets | 1 
  gluten-free  | 2
  gadget       | 3
  </code></pre>
  
  <p><code>inventory</code>:</p>
  
  <pre><code>item   | qty
  -------|----
  thing1 | 0
  thing2 | 325
  thing3 | 452
  thing5 | 123
  thing7 | 5
  </code></pre>
  
  <p><code>campaign_to_inventory</code>:</p>
  
  <pre><code>id | item
  ---|-------
  1  | thing1
  1  | thing2
  1  | thing5
  2  | thing1
  2  | thing3
  3  | thing7
  </code></pre>
  
  <p>I'd like to run a query to find all the campaigns I could run where I have the needed inventory in stock. I'm currently running this query:</p>
  
  <pre><code>SELECT * FROM `campaigns` LEFT JOIN `campaign_to_inventory` ON `campaigns`.`id` = `campaign_to_inventory`.`id` LEFT JOIN `inventory` ON `campaign_to_inventory`.`item` = `inventory`.`item`
  </code></pre>
  
  <p>Which returns:</p>
  
  <pre><code>name         | id | item   | qty
  -------------|----|--------|----
  blue-widgets | 1  | thing1 | 0
  blue-widgets | 1  | thing2 | 325
  blue-widgets | 1  | thing5 | 123
  gluten-free  | 2  | thing1 | 0
  gluten-free  | 2  | thing3 | 452
  gadget       | 3  | thing7 | 5
  </code></pre>
  
  <p>Should I use PHP to process this data to find only campaigns where all item quantities are greater than a minimum threshold, or is there a way to modify the query to limit the rows there? Is there a rule of thumb of when I can/should do it in one and not the other?</p>