本文介绍了分层查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,一个是admin,第二个是product. admin表包含parentid(表示自我连接).因此,我想获取一些根管理员名称为搜索条件的产品.我正在使用层次结构查询,您能否给我一些想法.如何解决它,因为我正在获取整个数据,但我不知道关键字(开头)应该给出什么.

表管理员

ID ParentId名称
1个空Jone
2 1玛丽亚
3空汤姆
4 2马丁
5 3携带



餐桌产品

ID AdminId名称
1 1本书
2 5笔
3 4车


结果:-
如果管理员名称为Jone,则产品应为书籍和汽车.
如果管理员名称是Tom,那么产品应该是pen.

I have two tables one is admin and second is product. admin table contain parentid ( means self join ) . So i want to fetch some product whose root admin name is search criteria. i am using hirarchy queries can you give me some idea. how to solve it because i am fetching whole data and i don''t know what should give with the (start with) keyword.

Table Admin

Id ParentId Name
1 null Jone
2 1 Mariya
3 null Tom
4 2 Martin
5 3 Carry



Table Product

Id AdminId Name
1 1 book
2 5 Pen
3 4 Car


Results:-
if Admin Name is Jone then product should be book and car.
if admin name is Tom then product should be pen.

推荐答案

SELECT adm.Name, prod.name
FROM Admin AS adm LEFT JOIN Product AS prod ON adm.Id = prod.AdminId
WHERE adm.Id IS NULL



下面的查询按名称...从管理表中获取所有数据...



Below query gets all data from Admin table by the name of ...

SELECT adm.Name, prod.name
FROM Admin AS adm LEFT JOIN Product AS prod ON adm.Id = prod.AdminId
WHERE adm.Name = @AdmName


这篇关于分层查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:51