如何查询表中的嵌套行以返回满足条件的所有行(或任何嵌套子项或嵌套父项)? [英] How to Query nested rows in a table to return all row (or any of there nested children or nested parents) which meets a condition?

查看:124
本文介绍了如何查询表中的嵌套行以返回满足条件的所有行(或任何嵌套子项或嵌套父项)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ID列的表,ParentID列引用同一个表中的父行和带有true或false值的IsFavorite列。



我需要一个查询来返回所有具有IsFavorite = True的行或任何嵌套的父行或任何嵌套的子行具有IsFavorite = True?



注意:

嵌套级别没有限制....可能是三个或五个级别或更多。



谢谢大家:)

I have a table with ID column, ParentID column which refer to a parent row in the same table and an IsFavorite column with a true or false value.

I need a query to return all rows which has IsFavorite = True or any of there nested parents rows or any of there nested children has IsFavorite = True?

Note:
There is no limit for nested levels.... may be three or five levels or more.

Thanks Guys :)

推荐答案

这称为递归父子关系。

参考给定链接

1: link

2: link
This is called recursive parent child relationship.
Refer given link
1: link
2: link


你现在面临什么类型的问题。根据您的要求,您不必担心parentId和Id,主要要求是基于IsFavorite列的过滤器,显然它应该返回父和子信息。



EX:< br $> b $ b

What type of issue are you facing now.? As per your requirement you need not worry about parentId and Id the main requirement is filter based on IsFavorite column obviously it should return the parent and child information.

EX:

select * from tablename
where IsFavorite= true





如果这不是您的期望,那么请使用一些示例数据详细说明您的要求。



If this is not your expecting then please elaborate your requirement with some sample data.


这篇关于如何查询表中的嵌套行以返回满足条件的所有行(或任何嵌套子项或嵌套父项)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆