简体   繁体   English

Postgres - 在返回多行后选择一行

[英]Postgres - Selecting a row after RETURNING multiple rows

My query is我的查询是

DELETE FROM event_comments WHERE thread_id = '1BZbAR'
returning parent_id, comment_id, thread_id

And it returns 3 rows它返回 3 行

null | 1 | 1BZbAR
1    | 2 | 1BZbAR
2    | 3 | 1BZbAR

How do I select the row where parent_id is null?如何选择 parent_id 为空的行?

I have tried:我试过了:

DELETE FROM event_comments WHERE thread_id = '1BZbAR'
returning MIN(parent_id), comment_id, thread_id
DELETE FROM event_comments WHERE thread_id = '1BZbAR'
returning HAVING parent_id is null, comment_id, thread_id
DELETE FROM event_comments WHERE thread_id = '1BZbAR'
returning parent_id, comment_id, thread_id as t
SELECT * FROM t WHERE parent_id is null

However all of them give them syntax errors.然而,它们都给了它们语法错误。 What should I do?我该怎么办?

You should be able to subquery your current delete statement, or put it into a CTE and query that:您应该能够对当前的删除语句进行子查询,或者将其放入 CTE 并查询:

WITH cte AS (
    DELETE
    FROM event_comments
    WHERE thread_id = '1BZbAR'
    RETURNING parent_id, comment_id, thread_id
)

SELECT parent_id, comment_id, thread_id
FROM cte
WHERE parent_id IS NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM