Postgresql查询删除所有行
Postgresql query deletes all rows
我在带有 using
子句、left join
和 where
子句的 PostgreSQL 函数中编写了一个简单的 delete
查询。但是查询没有考虑 where
条件。它删除所有行。
我写了两种类型的查询都产生相同的结果
查询 1
delete from "StockInfos" using "StockInfos" as si
left outer join "PurchaseOrderInfos" as poi on poi."Id" = si."PurchaseOrderInfoId"
left outer join "ReceivingInfos" as ri on ri."PurchaseOrderInfoId" = poi."Id"
where ri."Id" = (delete_data->>'Id')::bigint;
查询 2
delete from "StockInfos" where exists (
select * from "StockInfos" as si
left join "PurchaseOrderInfos" as poi on poi."Id" = si."PurchaseOrderInfoId"
left outer join "ReceivingInfos" as ri on ri."PurchaseOrderInfoId" = poi."Id"
where ri."Id" = (delete_data->>'Id')::bigint
);
我不明白这是什么问题。谁能告诉我出了什么问题?
我会用相关的子查询来重新表述。这使逻辑更清晰,并且应该做你想做的事:
delete from "StockInfos" si
where exists (
select 1
from "PurchaseOrderInfos" poi
inner join "ReceivingInfos" as ri on ri."PurchaseOrderInfoId" = poi."Id"
where
oi."Id" = si."PurchaseOrderInfoId"
and ri."Id" = (si.delete_data->>'Id')::bigint
)
我在带有 using
子句、left join
和 where
子句的 PostgreSQL 函数中编写了一个简单的 delete
查询。但是查询没有考虑 where
条件。它删除所有行。
我写了两种类型的查询都产生相同的结果
查询 1
delete from "StockInfos" using "StockInfos" as si
left outer join "PurchaseOrderInfos" as poi on poi."Id" = si."PurchaseOrderInfoId"
left outer join "ReceivingInfos" as ri on ri."PurchaseOrderInfoId" = poi."Id"
where ri."Id" = (delete_data->>'Id')::bigint;
查询 2
delete from "StockInfos" where exists (
select * from "StockInfos" as si
left join "PurchaseOrderInfos" as poi on poi."Id" = si."PurchaseOrderInfoId"
left outer join "ReceivingInfos" as ri on ri."PurchaseOrderInfoId" = poi."Id"
where ri."Id" = (delete_data->>'Id')::bigint
);
我不明白这是什么问题。谁能告诉我出了什么问题?
我会用相关的子查询来重新表述。这使逻辑更清晰,并且应该做你想做的事:
delete from "StockInfos" si
where exists (
select 1
from "PurchaseOrderInfos" poi
inner join "ReceivingInfos" as ri on ri."PurchaseOrderInfoId" = poi."Id"
where
oi."Id" = si."PurchaseOrderInfoId"
and ri."Id" = (si.delete_data->>'Id')::bigint
)