I have a SQL table which consists of order details.
Table Example
order_id | item_type | stock_code | price_code
----------------------------------------------
1 | S | 0005-01 | NULL
1 | P | NULL | PRC-1
1 | S | 0004 | NULL
2 | S | 0005-02 | NULL
2 | S | 0004 | NULL
I'm trying to return all order_id's where an order contains a stock code beginning with 0005 but not with a price code beginning with PRC
My attempt
SELECT order_id
FROM order_detail as ordDetail
WHERE EXISTS(
SELECT *
FROM order_detail as ordDetail2
WHERE ordDetail2.order_id = ordDetail.order_id
AND stock_code LIKE '0005-%'
AND price_code NOT LIKE 'PRC%'
)
For some reason, it seems to be ignoring the price_code NOT LIKE argument?
Expected results
order_id
--------
2
2
I'd love the order_id's to be unique too if that were possible :/