Zekiel
Zekiel

Reputation: 31

mysql query - exclude an entry

I have 2 tables, 1 table is order and another table is order_items. Order_items contains many records for a order_id from order. I would like to query table order so that, if the orders contain certain order_items (such as product_item = 'nameProduct'). it would exclude these orders out of the result. how should I do it?

My current queries are:

select * orders where order_id in (select order_id from order_items where product_item !='nameProduct');

this query not really working because the select order_id from order_items where product_item !='nameProduct' can still select entry that has same order_id but just has a different product_item

thanks in advance!

Upvotes: 0

Views: 160

Answers (1)

Martin Smith
Martin Smith

Reputation: 453287

SELECT  *
FROM    orders o
WHERE   NOT EXISTS ( SELECT *
                     FROM   order_items oi
                     WHERE  product_item = 'nameProduct'
                            AND oi.order_id = o.order_id )

Upvotes: 2

Related Questions