[英]How to exclude SQL query results
我有兩個表,每個表都有一列product_id
,我想做一個查詢,該查詢將排除兩個表中都有product_id
產品的結果。
偽示例:
SELECT *
FROM table1
WHERE product_id NOT IN table2
如果兩個表中都可以有product_id,而另一個表中不存在,則必須進行聯合,因為mysql不支持完全外部聯接:
select t1.product_id, 't1' as table_name
from table1 t1 left join table2 t2
on t1.product_id = t2.product_id where t2.product_id is null
union
select t2.product_id, 't2' as table_name
from table1 t1 right join table2 t2
on t1.product_id = t2.product_id where t1.product_id is null
嘗試這個:
select * from table1
where product_id not in (select product_id from table2) as p;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.