Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql查询删除所有行_Postgresql_Sql Delete - Fatal编程技术网

Postgresql查询删除所有行

Postgresql查询删除所有行,postgresql,sql-delete,Postgresql,Sql Delete,我用using子句、left join和where子句在PostgreSQL函数中编写了一个简单的delete查询。但是查询没有考虑where条件。它删除所有行 我编写了两种类型的查询,它们都产生相同的结果 问题1 问题2 我不明白是什么问题。谁能告诉我出了什么问题吗?我会用一个相关的子查询来重新表述这个问题。这使得逻辑更加清晰,并且应该做您想做的事情: delete from "StockInfos" si where exists ( select 1 from "Purch

我用using子句、left join和where子句在PostgreSQL函数中编写了一个简单的delete查询。但是查询没有考虑where条件。它删除所有行

我编写了两种类型的查询,它们都产生相同的结果

问题1

问题2


我不明白是什么问题。谁能告诉我出了什么问题吗?

我会用一个相关的子查询来重新表述这个问题。这使得逻辑更加清晰,并且应该做您想做的事情:

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

)

delete_date来自哪个表?与您的问题无关,但是:您应该真正避免那些可怕的引用标识符。他们的麻烦远比他们值得的多@来自StockInfosops的GMB!抱歉,delete_data是jsonb的postgres函数类型的参数,它包含一个ReceivingInfo Id,该Id应与ReceivingInfo表的Id匹配此查询如何完美工作。谢谢。请告诉我,select 1是什么意思?@Ajmal Hossain:如果子查询返回某个内容,则只需检查一下。因为我们不需要实际的结果集,所以我们使用select 1。这只是一个约定,可以是select 0、select*或select null。。。。
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

)