I have a MySQL Left Join Query that returns 1 row from table A and multiple rows from table B. I need to have multiple WHERE clauses on these rows and only return if all of the B rows match.
I have:
SELECT
mage_sales_order.entity_id
FROM mage_sales_order
LEFT JOIN mage_sales_order_item
ON mage_sales_order.entity_id = mage_sales_order_item.order_id
WHERE
mage_sales_order_item.sku NOT LIKE '1-%'
AND mage_sales_order_item.sku LIKE '2-%'
## Group order IDs together
GROUP BY entity_id
However this will return any order that has an item with the SKU starting with "2-" even if it has an item starting with "1-". I want it so that orders only with items starting with "2-" are returned.
For example:
Should be returned:
- Order 1 (mage_sales_order)
- 2-sampleA-sku (mage_sales_order_item)
- 2-sampleB-sku (mage_sales_order_item)
Should not be returned:
- Order 1 (mage_sales_order)
- 1-sample-sku (mage_sales_order_item)
- 2-sampleA-sku (mage_sales_order_item)
- 2-sampleB-sku (mage_sales_order_item)
I assume this has already been asked but trying to search for it is proving difficult.
ANDbefore themage_sales_order_item.sku NOT LIKE '1-%'?