SQL:使用公共表表达式递归获取父记录
假设您必须遵循以下表格,其中销售由产品组成,并且产品可以放置在多个类别中。其中类别具有如下层次结构:
Man
Shoes
Sport
Casual
Watches
Women
Shoes
Sport
Casual
Watches
表格:
Sale:
id name
1 Sale1
Product:
id saleidfk name
1 1 a
2 1 b
3 1 c
4 1 d
5 1 e
ProductCategory :
productid categoryid
1 3
2 3
3 4
4 5
5 10
Category:
id ParentCategoryIdFk name
1 null Men
2 1 Shoes
3 2 Sport
4 2 Casual
5 1 Watches
6 null Women
7 6 Shoes
8 7 Sport
9 7 Casual
10 6 Watches
问题:
现在在我的网站上,我想创建一个控件,其中仅显示特定销售的类别,并且类别中填充了销售的产品。我也想要 包括类别的层次结构。因此,如果我们有一个叶节点,请递归地到达顶部节点。
因此,对于 sale1,我应该有一个具有以下结果的查询:
Men
Shoes
Sport
Casual
Watches
Women
Watches
Suppose you have to following tables where a sale consists of products and a product can be placed in multiple categories. Whereby categories have a hierarchy structure like:
Man
Shoes
Sport
Casual
Watches
Women
Shoes
Sport
Casual
Watches
Tables:
Sale:
id name
1 Sale1
Product:
id saleidfk name
1 1 a
2 1 b
3 1 c
4 1 d
5 1 e
ProductCategory :
productid categoryid
1 3
2 3
3 4
4 5
5 10
Category:
id ParentCategoryIdFk name
1 null Men
2 1 Shoes
3 2 Sport
4 2 Casual
5 1 Watches
6 null Women
7 6 Shoes
8 7 Sport
9 7 Casual
10 6 Watches
Question:
Now on my website I want to create a control where only the categories are shown of a certain sale and where the categories are filled with the products of the sale. I also want
to include the hierarchy structure of the categories. So if we have a leaf node, recursively go up to the top node.
So with sale1 I should have a query with the following result:
Men
Shoes
Sport
Casual
Watches
Women
Watches
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这样的操作 - 获取类别的分层列表的基本 CTE 与此类似:
现在您需要做的是将其他表连接到此 CTE,最终获得以下查询:
这给了我一个结果输出类似:
Try something like this - the basic CTE to get a hierarchical listing of your categories would be similar to this:
Now what you need to do is join your other tables to this CTE, to get the following query in the end:
This gives me a resulting output something like:
我认为,如果您创建一个额外的表来列出每个类别的所有祖先类别(父母、祖父母等),您将获得最快的性能和更清晰的 SQL 查询,如下所示:
这将创建更多的 SQL插入新类别或删除它们时的开销,但可以让您更快地运行查询。
您可能需要考虑做的下一件事是将排名和级别列添加到类别(同样,创建和删除类别时需要做更多工作):
排名列指定排序顺序。
然后,您可以简单地运行以下查询:
希望这有帮助。
I think you'll get the fastest performance and also much cleaner SQL queries if you create an additional table that lists all the ancestor categories (parent, grandparent, etc.) for every category, something like this:
This will create a little more SQL overhead when you insert new categories or delete them, but will allow you to run you query much faster.
The next thing you may want to consider doing is adding rank and level columns to categories (again, more work when creating and deleting categories):
The rank column specifies the sort order.
Then, you can simply run the following query:
Hope this helps.
它不是特别有效,但是如果您想要做的是有效地“分解”整个层次结构并按顺序从父级到叶级获得结果,类似这样的事情就可以做到:
这应该会得到类似于以下内容的结果:
当然实际结果不会有这样的分隔符,我添加了这些分隔符以使结果的含义更清晰。
如果您不希望它像这样完全爆炸,您可以使用另一个 rownum 只返回每个父级的第一个实例:
...将为您提供类似于以下结果:
注意: 请小心第二个版本,因为它不一定保证按层次结构顺序返回结果。碰巧这个版本确实如此,因为 ID 本身是按层次顺序排列的。您可以绕过此限制,但它会给这个已经有些复杂的查询增加很多复杂性。
第二个版本确实保证主类别始终出现在其任何子类别之前,如果您计划使用字典构建递归数据结构,这很好。它可能不适合更快的基于堆栈的树构建或直接向用户报告。出于这些目的,您可能需要使用第一个版本。
It's not particularly efficient, but if what you want to do is effectively "explode" the entire hierarchy and get the results in sequence from parent to leaf, something like this would do it:
This should get you results similar to the following:
Of course the actual results won't have separators like that, I've added those to make the meaning of the results clearer.
If you don't want it completely exploded like this, you can use another rownum to only return the first instance of each parent:
...will give you results similar to:
Note: Be careful with the second version, as it is not necessarily guaranteed to return results in hierarchical order. It just so happens that this version does because the IDs themselves are in hierarchical order. You can get around this limitation, but it would add a lot more complexity to this already-somewhat-complex query.
The second version does guarantee that a master category will always appear before any of its subcategories, which is fine if you plan to build a recursive data structure using a dictionary. It just might not be suitable for faster stack-based tree building or direct-to-user reporting. For those purposes, you would want to use the first version instead.
有点混乱,但是:
需要注意的相关点是,为了使完整的层次结构有意义,您需要类别,无论它们是否有产品。此外,SortOrder 的 varchar 允许层次结构以正确的顺序显示。
Is a bit messy, but:
The pertinent points to notice are that to get the full hierarchy to make sense, you need the categories whether they have products or not. Also the varchar for the SortOrder allows the hierarchy to display in the correct order.
我想我已经太晚了,但对于未来尝试同样做法的同行来说,我认为这会起作用。 :)(只是针对一个特定项目的父层次结构执行此操作,但与叶子的内部连接将执行相同的操作)
I guess I'm too late, but for future peers trying the same, I think this will work. :) (Just did it for one specific item's parent hierarchy, but an inner join with leaves will do the same trick)