如何在嵌套集模型(MySQL)中插入数据; [英] How to insert data in the nested set model(MySQL);

查看:451
本文介绍了如何在嵌套集模型(MySQL)中插入数据;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌套集模型中,我们有LEFT和Right列

In the nested set model we have LEFT and Right columns

第一次当表是空的,我需要插入到RIGHT列,如果i不知道有多少孩子。

the first time when the table is empty, what i need to insert to the RIGHT column, if i don't know how many children will i have

LEFT 1 - 永远

LEFT 1 - forever

- 什么值在这里??

RIGHT ? - what value goes here??

如何使它动态?不是静态的。

how to make it dynamic? not static.

ps:使用php

推荐答案

假设从您的标签和标题,您正在寻找一个解决方案与 MySQL

I'm assuming from your tags and title that you are looking for a solution that works with MySQL.

是的,你是对的,除非你知道提前数量的元素 right 的值需要动态计算。有两种方法可以使用:

Yes, you are right that unless you know the number of elements in advance the value for right needs to be calculated dynamically. There are two approaches you can use:


  • 您可以从最小值开始(在这种情况下为2),然后增加

  • 你可以做一个像10000000这样的猜测,希望这是足够的,但你需要准备好的可能性,这是不够的,可能需要再次调整。 li>
  • You could start with the least value that works (2 in this case) and increase it later as needed.
  • You could just make a guess like 10000000 and hope that's enough, but you need to be prepared for the possibility that it wasn't enough and may need adjusting again later.

在这两种情况下,您需要实现 left right 可能需要在插入新行时调整多个行的值,但在第二种情况下,如果您的猜测错误,您只需要执行更新。因此,第二个解决方案更复杂,但可以提供更好的性能。

In both cases you need to implement that the left and right values for multiple rows may need to be adjusted when inserting new rows, but in the second case you only actually need to perform the updates if your guesses were wrong. So the second solution is more complex, but can give better performance.

请注意,在存储层次数据的四种常见方法中,嵌套集方法是最难的来执行插入和更新。请参阅Bill Karwin的边缘数据模型的幻灯片69。

Note that of the four common ways to store heirarchical data, the nested sets approach is the hardest to perform inserts and updates. See slide 69 of Bill Karwin's Models for Heirarchical Data.

这篇关于如何在嵌套集模型(MySQL)中插入数据;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆