从sql查询中获取所有父ID层次结构 [英] Get a all parent id hierarchy from sql query
本文介绍了从sql查询中获取所有父ID层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有以下表数据
UserId ParentId Name 和其他字段
1 3 abc
2 1 abc
3 2 abc
4 1 abc
5 1 abc
I有一个以下详细信息UserId = 5 AND ParentId = 2
比我想要的数据。
< pre lang =SQL> UserId ParentId Name
1 3 abc
3 2 abc
请帮帮我和评论Sql查询可以得到我的欲望输出。
先谢谢你..
解决方案
您好,
根据您提供的信息,请检查此...
SELECT UserId,ParentId,name 来自 yourTable WHERE UserId in
(
SELECT ParentId 来自 yourTable 其中 UserId = 5
)
union
SELECT * 来自 yourTable 其中 ParentId = 2
希望这能提供您所需的输出。
干杯
Hello Every one,
I have a Following table data
UserId ParentId Name and Other Field
1 3 abc
2 1 abc
3 2 abc
4 1 abc
5 1 abc
I have a Following Detail UserId = 5 AND ParentId = 2
Than I want Following Data.
UserId ParentId Name
1 3 abc
3 2 abc
Please help me and Comment Sql query that can get my desire output.
Thanks in Advance..
解决方案
Hi,
as per your info provided, check this...
SELECT UserId, ParentId,name from yourTable WHERE UserId in ( SELECT ParentId from yourTable where UserId=5 ) union SELECT * from yourTable where ParentId=2
Hope this will give your required output.
Cheers
这篇关于从sql查询中获取所有父ID层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文