当 Table1.COL = Table2.Col 时从 Table1 中删除行

Delete rows from Table1 when Table1.COL = Table2.Col

我有2个table(比较&comparison_2)。比较 table 有很多行,而 comparison_2 实际上是比较的一个小子集。这两个 table 都有两个相似的列,名为条码,我想从比较中删除所有那些与 comparison_2 具有匹配条码的行。 我希望我的问题很清楚。

我会用 exists:

delete from comparison
where exists (select 1 from comparison_2 c2 where c2.barcode = comparison.barcode)

这个查询:

SELECT barcodes FROM comparison 
EXCEPT 
SELECT barcodes FROM comparison_2

returns comparison 的所有 barcodes 不存在于 comparison_2.

在您的 DELETE 语句中使用它:

DELETE FROM comparison
WHERE barcodes NOT IN (
  SELECT barcodes FROM comparison 
  EXCEPT 
  SELECT barcodes FROM comparison_2
)

或者更简单:

DELETE FROM comparison
WHERE barcodes IN (SELECT barcodes FROM comparison_2)