使用 mysql 查询查找所有嵌套子类别的最不密集的方法

发布于 2024-09-03 10:02:56 字数 528 浏览 5 评论 0原文

我有一个数据库,其中的项目被分类。其中一些类别是嵌套的,例如:

Animals > Birds > Parrots  
Animals >  Birds > Penguin 
Animals > Mammals > Cats  
Animals > Mammals > Dogs 
Animals > Reptiles > Snakes 
Plants > Trees 
Plants > Flowers

我将这些类别放在表格中,沿着

CATEGORY    PARENT
Animals     -
Birds       Animals
Penguin     Birds

的行我希望能够以动物为起点并列出该类别下的所有子类别,因此对于动物,我们会列出鸟类、哺乳动物、爬行动物、鹦鹉、企鹅、猫、狗、蛇

这可以通过单个查询实现吗?如果没有,我需要做什么

TIA

I have a database in which items are placed into categories. Some of these categories are nested, so as an example:

Animals > Birds > Parrots  
Animals >  Birds > Penguin 
Animals > Mammals > Cats  
Animals > Mammals > Dogs 
Animals > Reptiles > Snakes 
Plants > Trees 
Plants > Flowers

etc

I have these in a table along the lines of

CATEGORY    PARENT
Animals     -
Birds       Animals
Penguin     Birds

etc

I'd like to be able to take the starting point of say, Animals and list all of the subcategories that come under that, so for animals we would have listed Birds, Mammals, Reptiles, Parrots, Penguin, Cats, DOgs, Snakes

Is this possible with a single query? If not, what would I need to do

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罪#恶を代价 2024-09-10 10:02:56

您可以摆脱递归并使用单个查询来查找子计数、直接类别和子类别,方法是按照下面的“修改的预序树遍历”文章,使用单个 SQL 查询:

  1. 管理 MySQL 中的分层数据

google.com 中搜索关键字“Modified Tree Traversal + Sitepoint”,阅读有关 sitepoint 的文章。 com 解释了相同的模式。

You can get away of recursion and use single queries to find child count, immediate categories and subcategories within using single SQL query by following the "Modified Preorder Tree Traversal" article below:

  1. Managing Hierarchical Data in MySQL

Search google.com for the keywords "Modified Tree Traversal + Sitepoint" to read the an article on sitepoint.com that explains the same pattern.

夜雨飘雪 2024-09-10 10:02:56

如果您向架构添加一些层次结构元数据,则可以通过单个查询高效地完成此操作。

将两个整数列(startend)添加到您的类别表中。然后对树进行深度优先遍历,在每一步递增一个计数器,并在进入节点时将计数器值分配给 start ,在离开节点时将计数器值分配给 end (即,当所有它的孩子已被处理)。

因此,对于您的示例,值显示为 (start,end)

Animals (1,18)
   Birds (2,7)
      Parrots (3,6 )
      Penguin (4,5)
   Mammals (8,13)
      Cats (9,12)
      Dogs (10,11)
   Reptiles (14,17)
      Snakes (15,16)
Plants (19,24)
   Trees (20,23)
   Flowers (21,22)

现在要选择 get Animals 及其子项,您可以运行如下查询:

SELECT * FROM Category where start >=1 and start < 18

您显然已经权衡了重建元数据的成本当层次结构发生变化而影响读取查询的效率时。对于相对静态的层次结构,这种技术效果相当好。

It is possible and efficient to do this with a single query if you add some hierarchy metadata to your schema.

Add two integer columns (start and end) to your category table. Then do a depth-first traversal of your tree incrementing a counter at each step and assigning the counter value to start when entering a node and to end when leaving (i.e. when all it's children have been processed).

So for your example, with the values shown as (start,end):

Animals (1,18)
   Birds (2,7)
      Parrots (3,6 )
      Penguin (4,5)
   Mammals (8,13)
      Cats (9,12)
      Dogs (10,11)
   Reptiles (14,17)
      Snakes (15,16)
Plants (19,24)
   Trees (20,23)
   Flowers (21,22)

Now to select get Animals and it's children you can just run something like this query:

SELECT * FROM Category where start >=1 and start < 18

You obviously have weigh the cost of rebuilding the metadata when the hierarchy changes against the efficiency for read queries. For relatively static hierarchies this technique works fairly well.

濫情▎り 2024-09-10 10:02:56

众所周知,SQL 不擅长遍历分层数据。

我会对此进行编程并递归或迭代地查找所有孩子。

下面的伪代码应该可以工作(如果数据中没有循环)

  • 将动物的孩子添加到数组 A
  • index = 0
  • while index <长度(A)
    • 将子项(A[i])追加到数组
    • 索引 = 索引 + 1

如果层次结构中存在循环,则必须确保不要追加已经存在于数组中的子级,否则循环将消耗所有内存并崩溃

SQL is notoriously bad at traversing hierarchical data.

I would program this and recursively or iteratively find all children.

the following pseudo code should work (if there are no loops in the data)

  • add children of animals to array A
  • index = 0
  • while index < length(A)
    • append children(A[i]) to array
    • index = index + 1

If there are loops in the hierarchy you must make sure you do not append children which are already in the array or the loop will consume all memory and crash

谎言月老 2024-09-10 10:02:56

我对获取数据的 mysql 程序不太了解,但我们可以在 PHP 的帮助下使用单个 mysql 查询获取嵌套数据,您可以对任何语言使用相同的逻辑。

http://www.maheshchari.com/php-nested-类别-with-single-mysql-query/

I don't know much about mysql procedures to get the data but we can get nested data using single mysql query with help of PHP, you can use same logic for any language.

http://www.maheshchari.com/php-nested-categories-with-single-mysql-query/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文