开发者

mysql query - exclude an entry

开发者 https://www.devze.com 2023-03-31 02:36 出处:网络
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 certai

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 b开发者_运维知识库ecause 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!


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

精彩评论

暂无评论...
验证码 换一张
取 消