如何解决如何重用使用“WITH”运算符声明的 Postgres 变量
我需要从两个表中删除记录,但我不能一致地执行它,因为从第一个表删除后,第二个表将没有数据要删除。 我试过以下:
WITH person_ids AS(select person_id from application_person
where application_id in (select disTINCT duplicate.id
from application duplicate inner join application application
on duplicate.document_id = application.document_id
where duplicate.modify_date < application.modify_date)),delete from application_person where application_person.person_id in (select person_id from person_ids);
delete from person where id in (select person_id from person_ids);
对于 person_ids 的第二次调用,我有 Query Failed: ERROR: relation "person_ids" does not exist
我做错了什么? 谢谢。
解决方法
我做错了什么?
您有两个单独的声明。 person_ids
仅在第一个范围内,一直持续到分号。
你会想要使用
WITH duplicate_applications AS (
select DISTINCT duplicate.id
from application duplicate
inner join application application using (document_id)
where duplicate.modify_date < application.modify_date)
),deleted_persons AS (
delete from application_person
where application_id in (select application_id from duplicate_applications)
returning person_id
)
delete from person
where id in (select person_id from deleted_persons);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。