给定一条消息的入口点的 SQL 2008 消息线程递归查询
也许是一个过于复杂的标题。基本上可以想象一个电子邮件收件箱。我有一张桌子 如下:
正如你所看到的,这是一个递归表,非常简单,只有一条消息的parentID ,正如您通过绿色突出显示环所看到的,当parentID 为NULL 时,“链”就结束了。
我需要的是提供(例如)INBOXID 12,并返回所有父项。在这个示例中,我应该返回 1 条记录,即 INBOXID 11。
在第二个示例中,我应该能够传入 INBOXID 9,但这次我应该返回行 INBOXID 8,7 和 1
我确实通过以下查询取得了一些成功:
with q as
(
select inboxid, parentid
from bizzbox
union all
select a.inboxid, a.parentid
from bizzbox a
inner join q on q.inboxID = a.parentID
)
select distinct * from q
..但当然它返回了所有任何行的父级..我知道这可能是非常愚蠢的简单的事情,比如其中一个选择上的 where 子句..但是尝试过(即参数化起始点 inboxid 的传入),我不能很清楚我需要做什么???
任何帮助非常感谢!!!! 大卫.
Maybe an over elaborate title. Basically think of an email inbox. I have a table
as so:
As you can see, it is a recursive table, very simple, just has the parentID of a message, and as you can see with the green highlight ring, the end of the "chain" is when there is a NULL for the parentID.
What I need is to provide (for example) the INBOXID of 12, and return back all parents.. in this example I should get 1 record back which is INBOXID of 11.
In the second example, I should be able to pass in INBOXID of 9, but this time I should get back rows INBOXID 8,7 and 1
I did have some success with the following query:
with q as
(
select inboxid, parentid
from bizzbox
union all
select a.inboxid, a.parentid
from bizzbox a
inner join q on q.inboxID = a.parentID
)
select distinct * from q
.. but of course it returns all of the parents for any of the rows.. I know it is probably something really stupidly simple like a where clause on one of the selects.. but having tried it (i.e. to parameterize the passing in of the start point inboxid), I can't quite see what I need to do???
Any help much appreciated!!!!!
David.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试用这个:
Try with this: