正在根据另一个 table 中的条件删除 table 个条目

Deleting table entries based on criteria in another table

我们希望能够根据另一个 table 中设置的删除条件从 MySQL table 中删除条目。让我用一个例子来解释。
我有两个 table 定义如下:

CREATE TABLE base_tbl
(
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  f1 VARCHAR(8),
  f2 VARCHAR(8),
  
  PRIMARY KEY (id)
);


CREATE TABLE del_criteria_tbl
(
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  f3 VARCHAR(8),
  f4 VARCHAR(8),
  
  PRIMARY KEY (id)
);

base_tbl 有数据,del_criteria_tbl 有从 base_tbl.

中删除条目的条件

我按如下方式填充 tables:

INSERT INTO base_tbl(f1, f2) VALUES ('ABC', '123C57'),
('ABC', '532B49'), ('DEF', '397F45'),
('DEF', '684G65'), ('GHI', '793A86'),
('GHI', '541H32');  

INSERT INTO del_criteria_tbl(f3, f4) VALUES ('ABC', '532B49'),
('ABC', '813E89'), ('DEF', '397F45'),
('GHI', '541H32');

显然:

mysql>SELECT * FROM base_tbl;
+----+------+--------+
| id | f1   | f2     |
+----+------+--------+
|  1 | ABC  | 123C57 |
|  2 | ABC  | 532B49 |
|  3 | DEF  | 397F45 |
|  4 | DEF  | 684G65 |
|  5 | GHI  | 793A86 |
|  6 | GHI  | 541H32 |
+----+------+--------+


mysql>SELECT * FROM del_criteria_tbl;
+----+------+--------+
| id | f3   | f4     |
+----+------+--------+
|  1 | ABC  | 532B49 |
|  2 | ABC  | 813E89 |
|  3 | DEF  | 397F45 |
|  4 | GHI  | 541H32 |
+----+------+--------+

我想定义一个简洁高效的 SQL 操作来执行以下伪SQL 逻辑:

DELETE FROM base_tbl WHERE base_tbl.f1 = del_criteria_tbl.f3 AND base_tbl.f2 = del_criteria_tbl.f4

操作执行后,SELECT * FROM base_tbl应该产生:

+----+------+--------+
| id | f1   | f2     |
+----+------+--------+
|  1 | ABC  | 123C57 |
|  4 | DEF  | 684G65 |
|  5 | GHI  | 793A86 |
+----+------+--------+

一个简单的方法是IN:

DELETE b FROM base_tbl b
    WHERE (b.f1, b.f2) IN (SELECT dc.f3, dc.f4
                           FROM del_criteria_tbl dc
                          );

使用 (f1, f2) 上的索引,您可能会发现 JOIN 具有更好的性能:

DELETE b
    FROM base_tbl b JOIN
         del_criteria_tbl dc
         ON b.f1 = dc.f3 AND b.f2 = c.f4;

我会推荐 exists:

delete b
from base_tbl b
where exists (
    select 1
    from del_criteria_tbl dc
    where dc.f1 = b.f1 and dc.f2 = b.f2
)

这似乎是表达您的要求的最自然方式。在大型数据集上,exists 通常比 in 更好。为了提高性能,您需要 del_criteria_tbl(f1, f2).

上的索引