简体   繁体   English

PostgreSQL:根据邻接模型中的子列更新父列

[英]Postgresql: Update parent column based on child column in adjacency model

I have a table which models a hierarchical data using adjacency model. 我有一个表,该表使用邻接模型对分层数据进行建模。

|----------|--------|--------|--------| | ParentID | ID | Flag | Name | |----------|--------|--------|--------| | 0 | 1 | | x | |----------|--------|--------|--------| | 1 | 2 | | y | |----------|--------|--------|--------| | 2 | 3 | | z | |----------|--------|--------|--------| I have a SELECT query which sets the Flag if name does not matches certain patterns. 我有一个SELECT查询,如果名称与某些模式不匹配,它将设置Flag。

SELECT r.id, r.pid, r.name, r.name NOT LIKE ALL(ARRAY[patterns to
match]) AS r.flag FROM TABLE1 AS r

Now i want to flag the parents also if the child was flagged. 现在如果孩子被举报了,我也想举报父母。 How can i acheive that? 我该如何实现?

A left self join will allow you to find the parent fields. left self join将使您能够找到父字段。 BOOL_OR can be used to aggregate the children if there are more than one. 如果有多个BOOL_OR则可以使用BOOL_OR来聚合子代。

SELECT r.id, r.pid, r.name,
CASE WHEN r.name NOT LIKE ALL(ARRAY[patterns to match]) THEN True ELSE 
BOOL_OR(child.name NOT LIKE ALL(ARRAY[patterns to match])) END AS r.flag
FROM TABLE1 AS r
LEFT JOIN TABLE1 as child on child.pid=r.id
GROUP BY r.id, r.pid, r.name, r.name NOT LIKE ALL(ARRAY[patterns to match])

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

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