我有两个表,每个表都有一列product_id
,我想做一个查询,它会排除两个表中找到product_id
的产品的结果。
伪示例:
SELECT *
FROM table1
WHERE product_id NOT IN table2
答案 0 :(得分:1)
如果两个表中的product_ids都不存在于另一个表中,则必须进行联合,因为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
答案 1 :(得分:0)
试试这个:
select * from table1
where product_id not in (select product_id from table2) as p;