SQL:如何删除也在另一个 table 中的行?
SQL: How do I delete rows that are also in another table?
假设我有两张桌子。例如,
Table 1:
Store Product
1 2
1 1
2 3
2 4
和Table 2
Store Product
1 2
2 3
如何删除 Table 1 中也在 Table 2 中的所有行?
因此,新的 Table 1 将是:
Store Product
1 1
2 4
你似乎想要 :
select t1.*
from table1 t1
where not exists (select 1
from table2 t2
where t2.store = t.store and
t2.product = t1.product
);
同样删除版本为:
delete
from table1 t1
where exists (select 1
from table2 t2
where t2.store = t.store and
t2.product = t1.product
);
你可以这样使用:
DELETE
t1
FROM
table1 t1
WHERE
(t1.store, t1.product) IN ( SELECT
t2.store,
t2.product
from
table2 t2);
假设我有两张桌子。例如,
Table 1:
Store Product
1 2
1 1
2 3
2 4
和Table 2
Store Product
1 2
2 3
如何删除 Table 1 中也在 Table 2 中的所有行?
因此,新的 Table 1 将是:
Store Product
1 1
2 4
你似乎想要 :
select t1.*
from table1 t1
where not exists (select 1
from table2 t2
where t2.store = t.store and
t2.product = t1.product
);
同样删除版本为:
delete
from table1 t1
where exists (select 1
from table2 t2
where t2.store = t.store and
t2.product = t1.product
);
你可以这样使用:
DELETE
t1
FROM
table1 t1
WHERE
(t1.store, t1.product) IN ( SELECT
t2.store,
t2.product
from
table2 t2);