title: Select all orders that have the exact same items as this order with MySQL
body: <p>I'm kind of stumped on this one. What I'd like to do is get all Orders that have the exact identical items and quantities to a given Order.</p>
  
  <p>For example, Order number 100 has 1 Blue Widget, 2 Pink Widgets, and 1 Green Widget. I want to select all Orders from the database that also have exactly 1 Blue Widget, 2 Pink Widgets, and 1 Green Widget, and no items more or less than that.</p>
  
  <p>The goal is to answer the question, "the last time this exact combination of items was shipped, what box did we ship it in?". If I can get orders that are an exact match in terms of items, I can see what box was used before.</p>
  
  <p>My Table structure is:</p>
  
  <p><strong>Orders_Items</strong></p>
  
  <pre><code>+---------------------------+-----------+-------------+
  | OrderID   | OrderPartID   |   ItemID  | QtyOrdered  |
  +---------------------------+-----------+-------------+
  | 1         |   21          |      4    | 11.5000     |
  | 2         |    8          |      5    |  4.3333     |
  +---------------------------+-----------+-------------+
  </code></pre>
  
  <p><br>
  <strong>Orders_OrderParts</strong></p>
  
  <pre><code>+--------------+------------+-----------+
  | OrderPartID  | OrderID    |  StatusID |
  +--------------+------------+-----------+
  | 1            |   21       |    4      |
  | 2            |    8       |    5      |
  +---------------------------+-----------+
  </code></pre>
  
  <p><br>
  <strong>Orders_OrderParts_2_Shipments</strong></p>
  
  <pre><code>+--------------+------------+--------------+
  | OrderPartID  | OrderID    |  ShipmentID  |
  +--------------+------------+--------------+
  | 1            |   21       |      4       |
  | 2            |    8       |      5       |
  +---------------------------+--------------+
  </code></pre>
  
  <p>The "ShipmentID" is the magic number we're trying to find since it will tell me which boxes were used in the past.</p>
  
  <p>I've started with this as a basis.</p>
  
  <pre><code>SELECT *, COUNT(*) AS ItemsInOrder
  FROM Orders_Items OI
  LEFT JOIN Orders_OrderParts OP
     ON OP.OrderPartID = OI.OrderPartID
  LEFT JOIN Orders_OrderParts_2_Shipments OP2S
     ON OP2S.OrderPartID = OP.OrderPartID
  WHERE OP.StatusID=800 # Only select Orders that have shipped.
  GROUP BY OI.OrderPartID
  </code></pre>
  
  <p>This is a working query, but for now, it only returns all items grouped by order part. I need to remove all the order parts that don't contain the items in the current order part.</p>
  
  <p>The Query:</p>
  
  <pre><code>SELECT * FROM `Orders_Items` WHERE OrderPartID = 100
  </code></pre>
  
  <p>returns all of the items in the current order part and their quantities, ie:</p>
  
  <pre><code>+--------------+------------+--------------+
  | OrderPartID  |   ItemID   |  Quantity    |
  +--------------+------------+--------------+
  | 100          |     21     |      1       |
  | 100          |     8      |      3       |
  +---------------------------+--------------+
  </code></pre>
  
  <p>So I need to narrow the first query by showing only order parts that have exactly 1 of Item #21, and exactly 3 of Item #8.</p>
  
  <p>And that's where I'm stuck. I could do the two queries separately and use PHP to loop over the results and compare them, but I'm curious if it would be more efficient to use a pure MySQL solution... or if a single query is even possible in this situation?</p>