![](/img/trans.png)
[英]How to COUNT the number of rows where a column matches a column of another table
[英]How to delete rows where more than 1 column matches another table?
我有两张桌子。 一个(我们称它为 table1)看起来有点像这样:
account_number | offer_code
---------------|-----------
1 | 123
1 | 456
2 | 123
另一个表(我们称它为 table2)看起来有点像这样:
account_number | offer_code
---------------|-----------
1 | 123
我想从 table1 中删除所有行,其中 account_number 和 offer_code 与 table2 中的一行匹配。 所以之后 table1 看起来像这样:
account_number | offer_code
---------------|-----------
1 | 456
2 | 123
我试过以下,但它没有运行:
DELETE
FROM TABLE1 A
INNER JOIN
TABLE2 B
ON A.ACCOUNT_NUMBER = B.ACCOUNT_NUMBER
AND A.OFFER_CODE = B.OFFER_CODE
;
我也试过以下。 它似乎可以运行,但是两个表中的庞大数据量(table1 中的 65.5m 行和 table2 中的 9m 行)意味着这样做需要很长时间(我被迫在 3 小时后终止查询)。
DELETE
FROM TABLE1
WHERE CONCAT(ACCOUNT_NUMBER, OFFER_CODE) IN
(
SELECT CONCAT(ACCOUNT_NUMBER, OFFER_CODE)
FROM TABLE2
)
;
有谁知道是否有办法有效地完成这项工作?
数据库不喜欢更新和删除过程。 他们很累。 根据您的应用程序(仔细检查:!!),您可以尝试以下操作:
create table table1_tmp
select * from table1
minus
select * from table2;
alter table table1 rename to table1_tmp2;
alter table table1_tmp rename to table1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.