mySQL 传递闭包表
我在 SQL Server 中使用了一些代码,从另一个仅具有直接父/子关系的表生成闭包表,我可以对此运行非常简单的查询来确定沿袭。现在我需要在 mySQL 中完成所有这些操作,但是我在递归查询生成闭包表时遇到了麻烦...
我原来的 SQL 服务器查询是
WHILE @@ROWCOUNT>0
INSERT INTO [ClosureTable] ([Ancestor], [Descendent])
SELECT distinct [Parent],[tc].[Descendent]
FROM
[RelationshipTable]
INNER JOIN [ClosureTable] as tc
ON [Child]COLLATE DATABASE_DEFAULT =
[tc].[Ancestor]COLLATE DATABASE_DEFAULT
LEFT OUTER JOIN [ClosureTable] As tc2
ON [Parent]COLLATE DATABASE_DEFAULT =
[tc2].[Ancestor] COLLATE DATABASE_DEFAULT
AND [tc].[Descendent]COLLATE DATABASE_DEFAULT =
[tc2].[Descendent]COLLATE DATABASE_DEFAULT
我的第一个问题是找到 @@ROWCOUNT 的替代品...但也许mySQL中的递归查询完全不同?我还查看了 Bill Karwin 的演示
PS。由于性能问题,我需要“COLLATE DATABASE_DEFAULT”。
谢谢。
I have some code I've been using in SQL Server to generate a closure table from another table that has just the direct parent/child relationships, I can run very simple queries against this to determine lineage. Now I am needing to do all this in mySQL, but I am having trouble with the recursive querying to generate the closure table...
My original SQL server query is
WHILE @@ROWCOUNT>0
INSERT INTO [ClosureTable] ([Ancestor], [Descendent])
SELECT distinct [Parent],[tc].[Descendent]
FROM
[RelationshipTable]
INNER JOIN [ClosureTable] as tc
ON [Child]COLLATE DATABASE_DEFAULT =
[tc].[Ancestor]COLLATE DATABASE_DEFAULT
LEFT OUTER JOIN [ClosureTable] As tc2
ON [Parent]COLLATE DATABASE_DEFAULT =
[tc2].[Ancestor] COLLATE DATABASE_DEFAULT
AND [tc].[Descendent]COLLATE DATABASE_DEFAULT =
[tc2].[Descendent]COLLATE DATABASE_DEFAULT
My first problem is finding a substiture for @@ROWCOUNT... but perhaps recursive queries are completely different in mySQL? I've also checked out Bill Karwin's presentation
PS. The "COLLATE DATABASE_DEFAULT" was something I needed due to performance issues..
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这已经很旧了,但我觉得您仍然需要为其他人提供答案,以下是我从标准邻接表生成闭包表的方法:
I know this is old, but I feel you still need an answer on this for others looking, here is how I generated my closure table from my standard adjacency table:
不确定我是否理解你的确切问题是什么 - 我认为这是从邻接列表表递归生成一棵树 - 如果是这样,以下内容可能会有所帮助,但它不是递归的(真可惜!)
not sure if i understood what your exact problem is - i think it's around recursively generating a tree from an adjacency list table - if so, the following may help but it's not recursive (what a shame !!)