根据另一个 table 的值有条件地删除行
Conditional row deleting based on value of another table
我是 PostgreSQL 的新手,我对条件删除有疑问。
我有一个数据透视表 table:: TableA -> Spec_id-Status_id
我有另一个数据透视表 table:: TableB -> Spec_id-Trend_id
我需要根据以下条件删除 TableB 上的行:
如果在表 B 中,Spec_id(在表 A 中有 Status_id == 1)有 Trend_id == 4,则必须删除行 Spec_id-Trend_id .
我知道是这样的:
DELETE from TableB USING TableA WHERE TableA.Status_id == 1
但我不知道如何引用与该选择相关的 Spec_id。
您必须连接两个表:
DELETE FROM TableB
USING TableA
WHERE TableA.Spec_id = TableB.Spec_id
AND TableA.Status_id = 1;
我是 PostgreSQL 的新手,我对条件删除有疑问。
我有一个数据透视表 table:: TableA -> Spec_id-Status_id
我有另一个数据透视表 table:: TableB -> Spec_id-Trend_id
我需要根据以下条件删除 TableB 上的行:
如果在表 B 中,Spec_id(在表 A 中有 Status_id == 1)有 Trend_id == 4,则必须删除行 Spec_id-Trend_id .
我知道是这样的:
DELETE from TableB USING TableA WHERE TableA.Status_id == 1
但我不知道如何引用与该选择相关的 Spec_id。
您必须连接两个表:
DELETE FROM TableB
USING TableA
WHERE TableA.Spec_id = TableB.Spec_id
AND TableA.Status_id = 1;