sql select puzzle:当父项被过滤掉时删除子项
我本质上有一个表:
name has_children parent_id row_id values0.....valuesn
parent 1 1 1
children 0 1 2
children 0 1 3
parent 0 4 4
parent 1 5 5
children 0 5 6
children 0 5 7
子项的值可以与父项的值不同。 我想要一些选择/联接,它们将在值列(即> 10)上过滤表,并且如果其中一个子级对于过滤器为真,则将返回父级(即使过滤器为假)。
acceptable return:
parent=true all children=false, return just parent
parent=false >=1 children=true, return parent and all non-filtered child
我确信以前已经考虑过这个问题,但我不知道如何表达这个问题以找到解决方案。
I have a table essentially:
name has_children parent_id row_id values0.....valuesn
parent 1 1 1
children 0 1 2
children 0 1 3
parent 0 4 4
parent 1 5 5
children 0 5 6
children 0 5 7
the values for the children can be different than the values for the parent.
i want some selects/joins that will filter the table on a value column (i.e. >10) and will return the parent (even if false for the filter) if one of it's children is true for the filter.
acceptable return:
parent=true all children=false, return just parent
parent=false >=1 children=true, return parent and all non-filtered child
i'm sure this has been thought about before but i don't have the faintest idea how to phrase the question to find a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
符合 ANSI 标准。每个特定的 DBMS 可能有更快的实现
更好地编写为 JOIN
ANSI compliant. Each specific DBMS may have a faster implementation
Better written as a JOIN
将其作为两个单独的查询并在它们之间使用 UNION 来处理可能是最简单的。
It is probably easiest to deal with this as two separate queries with a UNION between them.