繁体   English   中英

PostgreSQL从结果中选择for循环

[英]Postgresql select from results in for loop like

我的英语不是最好的,但我会尽力的。 我有带有评论和回复的表格,我想从评论中选择父级LIMIT 10,然后选择与这些评论相关的回复。

  parentid  |   replyid   |  commentowner  |         commentbody         |        postcreation        
------------+-------------+----------------+-----------------------------+----------------------------
 h0rfizsUF6 | CGTSh5XLCB  | mary@none.com  | anyone want to make flowers | 2018-04-30 21:35:53.502332
            | CGTSh5XLCB  | bob@none.com   | reply to mary about flowers | 2018-04-30 21:39:04.313967
            | CGTSh5XLCB  | mary@none.com  | ok well sign up             | 2018-04-30 21:39:33.376884
 Zasrw8768F | DAeing34355 | james@none.com | Hey everyone!               | 2018-04-30 21:40:44.777557
 Zasr2222F  | DAeingrrrr  | mary@none.com  | yo yo yo all                | 2018-04-30 21:41:33.800034
            | CGTSh5XLCB  | james@none.com | Im signed up already        | 2018-04-30 21:42:03.771954
            | DAeingrrrr  | jimmy@none.com | in what house               | 2018-04-30 21:43:10.992619
(7 rows)

我想要的是:

  parentid  |   replyid   |  commentowner  |         commentbody         |        postcreation        
------------+-------------+----------------+-----------------------------+----------------------------
 h0rfizsUF6 | CGTSh5XLCB  | mary@none.com  | anyone want to make flowers | 2018-04-30 21:35:53.502332
            | CGTSh5XLCB  | bob@none.com   | reply to mary about flowers | 2018-04-30 21:39:04.313967
            | CGTSh5XLCB  | mary@none.com  | ok well sign up             | 2018-04-30 21:39:33.376884
            | CGTSh5XLCB  | james@none.com | Im signed up already        | 2018-04-30 21:42:03.771954
 Zasr2222F  | DAeingrrrr  | mary@none.com  | yo yo yo all                | 2018-04-30 21:41:33.800034
            | DAeingrrrr  | jimmy@none.com | in what house               | 2018-04-30 21:43:10.992619

当我得到父母身份时,我会得到这个

socialnetwork=# select parentid from comments where commentowner ='mary@none.com' and parentid IS NOT NULL;
  parentid  
------------
 h0rfizsUF6
 Zasr2222F

但我无法通过此返回执行forloop。

ERROR:  more than one row returned by a subquery used as an expression

尚不清楚您要做什么,但可能类似以下内容:

with parents as (
    select distinct replyid
    from comments
    where commentowner ='mary@none.com' and parentid IS NOT NULL
)
select C.* from parents P left join comments C on P.replyid = C.replyid
;

这不是十个父母ID,但是您的示例表的父母ID列中为空,并且尚不清楚行如何关联在一起。 相同的replyid列也令人困惑。 也许这些应该是parentid或commentid。

无论如何,您都可以使用CTE构造您感兴趣的键集,然后对表进行联接以获取所需的行。

暂无
暂无

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

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