Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.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

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中)_Sql_Postgresql_Subquery_Sql Delete - Fatal编程技术网

使用生成两列的子查询删除(在Postgresql中)

使用生成两列的子查询删除(在Postgresql中),sql,postgresql,subquery,sql-delete,Sql,Postgresql,Subquery,Sql Delete,我必须删除一些与使用子查询计算的两列相匹配的记录 我可以通过此查询正确地看到它们: select * from user_assignments as ua, ( select user_assignments.user_id as uid, job_selection as jid from user_assignments join job_selections on job_id = job

我必须删除一些与使用子查询计算的两列相匹配的记录

我可以通过此查询正确地看到它们:

select * from user_assignments as ua, 
    (
        select user_assignments.user_id as uid, 
               job_selection as jid 
        from user_assignments
            join job_selections on job_id = jobs.id
            join data on job_selections.data_id = data.id
            where data.my_column IS NULL
    ) as sq
    where sq.uid = ua.user_id AND ua.job_selection_id = sq.jid;
这很有效,我看到了我想删除的7个作业

然而,删除并不像更改“按删除选择”那样简单

如果我这样做:

delete from user_assignments as ua, 
    (
        ...
    ) as sq
    where sq.uid = ua.user_id AND sq.jid = ua.job_selection_id;
我得到:

ERROR:  syntax error at or near ","
我试过各种各样的组合,但都没能成功。我想这一定很简单,但我是SQL的新手

基本上,我有一个子查询,它正确地生成了两列,我可以使用它们从用户分配中选择,现在我想从用户分配中删除我知道可以选择的记录


如有任何提示,将不胜感激。提前感谢。

使用
中的
存在

delete from user_assignments ua
where exists (select 1
              from user_assignments ua2 join
                   job_selections js
                   on ua2.job_id = js.id join
                   data d
                   on js.data_id = d.id
              where d.my_column IS NULL and
                    ua.user_id = sq.uid and ua.job_selection_id = sq.jid
             );
哦,我知道了(我想)

请参阅本教程,特别是SQL部分使用别名子查询删除记录

如果其他人感兴趣,我所做的是:

DELETE FROM user_assignments ua
WHERE EXISTS(
    SELECT user_assignments.user_id as uid,
           user_assignments.job_selection as jid
    FROM user_assignments
        join job_selections on job_id = jobs.id
        join data on job_selections.data_id = data.id
    WHERE data.my_column IS NULL
        AND ua.user_id = uid
        AND ua.job_selection = jid
)

此查询也适用于
SELECT*FROM user\u assignments

Oh。。。当我发布我的答案时,我刚刚看到了这个答案。非常感谢。它看起来和我最终做的事情很相似,我相信这一个更安全,所以让它成为选择的答案。我信任你胜过信任我自己