处理mysql中的嵌套集?

2023-11-27

我决定跟随http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html

所以现在我正在寻找有关代码的帮助。

我正在使用他们的数据进行测试, 所以,我想象这棵树是这样的:

    array('value' => 'Richard Shakespeare',
        array('value' => 'Henry',
            array('value' => 'Joan'),
            array('value' => 'Margaret'),
            array('value' => 'William',
                array('value' => 'Susana',
                    array('value' => 'Elizabeth Hall',
                        array('value' => 'John Bernard'))),
                array('value' => 'Hamnet'),
                array('value' => 'Judith',
                    array('value' => 'Shakespeare Quiney'),
                    array('value' => 'Richard Quiney'),
                    array('value' => 'Thomas Quiney'))),
            array('value' => 'Gilbert'),
            array('value' => 'Joan',
                array('value' => 'William Hart'),
                array('value' => 'Mary Hart'),
                array('value' => 'Thomas Hart'),
                array('value' => 'Micheal Hart')),
            array('value' => 'Anne'),
            array('value' => 'Richard'),
            array('value' => 'Edmond')),
        array('value' => 'John'));

因此,如果我们想将其插入到数据库中,我们希望最终得到

Array
(
    [0] => Array
        (
            [value] => Richard Shakespeare
            [left] => 1
            [right] => 46
        )

    [1] => Array
        (
            [value] => Henry
            [left] => 2
            [right] => 43
        )

    [2] => Array
        (
            [value] => Joan
            [left] => 3
            [right] => 4
        )

    [3] => Array
        (
            [value] => Margaret
            [left] => 5
            [right] => 6
        )

    [4] => Array
        (
            [value] => William
            [left] => 7
            [right] => 24
        )

    [5] => Array
        (
            [value] => Susana
            [left] => 8
            [right] => 13
        )

    [6] => Array
        (
            [value] => Elizabeth Hall
            [left] => 9
            [right] => 12
        )

    [7] => Array
        (
            [value] => John Bernard
            [left] => 10
            [right] => 11
        )

    [8] => Array
        (
            [value] => Hamnet
            [left] => 14
            [right] => 15
        )

    [9] => Array
        (
            [value] => Judith
            [left] => 16
            [right] => 23
        )

    [10] => Array
        (
            [value] => Shakespeare Quiney
            [left] => 17
            [right] => 18
        )

    [11] => Array
        (
            [value] => Richard Quiney
            [left] => 19
            [right] => 20
        )

    [12] => Array
        (
            [value] => Thomas Quiney
            [left] => 21
            [right] => 22
        )

    [13] => Array
        (
            [value] => Gilbert
            [left] => 25
            [right] => 26
        )

    [14] => Array
        (
            [value] => Joan
            [left] => 27
            [right] => 36
        )

    [15] => Array
        (
            [value] => William Hart
            [left] => 28
            [right] => 29
        )

    [16] => Array
        (
            [value] => Mary Hart
            [left] => 30
            [right] => 31
        )

    [17] => Array
        (
            [value] => Thomas Hart
            [left] => 32
            [right] => 33
        )

    [18] => Array
        (
            [value] => Micheal Hart
            [left] => 34
            [right] => 35
        )

    [19] => Array
        (
            [value] => Anne
            [left] => 37
            [right] => 38
        )

    [20] => Array
        (
            [value] => Richard
            [left] => 39
            [right] => 40
        )

    [21] => Array
        (
            [value] => Edmond
            [left] => 41
            [right] => 42
        )

    [22] => Array
        (
            [value] => John
            [left] => 44
            [right] => 45
        )

)

所以我想到的问题是,如何最好地做到这一点?

我的解决方案是:

$container = array();

function children($item){
  $children = 0;
  foreach($item as $node)
    if(is_array($node))
      $children += children($node)+1;
    return $children;
}

function calculate($item, &$container, $data = array(0,0)){
  //althought this one is actually of no use, it could be useful as it contains a count 
  $data[0]++; //$left

  $right = ($data[0]+(children($item)*2))+1;

  //store the values in the passed container
  $container[] = array(
    'value' => $item['value'],
    'left'  => $data[0],
    'right' => $right,
  );

  //continue looping
  $level = $data[1]++;
  foreach($item as &$node)
    if(is_array($node))
      $data = calculate($node, $container, $data);

    $data[1] = $level;
    $data[0]++;
    return $data;
}

calculate($tree, $container);

效率如何我不知道。

但现在进入查询。

要选择节点的所有后代,我们可以使用

SELECT child.value AS 'Descendants of William', COUNT(*) AS `Level`
FROM tester AS parent
JOIN tester AS child ON child.`left` BETWEEN parent.`left` AND parent.`right`
WHERE parent.`left` > 7 AND parent.`right` < 24
GROUP BY child.value ORDER BY `level`;

要选择节点的所有后代,我们可以使用特定深度
请注意,我们选择 William 的后裔,深度为 2
威廉姆斯左:7,威廉姆斯右:24,级别:2

SELECT child.value AS 'Descendants of William', COUNT(*) AS `Level`
FROM tester AS parent
JOIN tester AS child ON child.`left` BETWEEN parent.`left` AND parent.`right`
WHERE parent.`left` > 7 AND parent.`right` < 24
GROUP BY child.value HAVING `level` <= 2 ORDER BY `level`;

所以这很容易。

但现在我想知道一些事情
请注意,在实际数据库中以及左/右所有行都有一个唯一的 id 和一个包含其邀请者 id 的“父”列,如果未受邀请则为 null

  • 假设我想插入David作为一个孩子Judith, 我怎么做?
  • 可以说我想得到Mary Hart's父母,以及父母 父母(array('Henery', 'Joan', 'Mary Hart')), 我怎么做?
  • 假设我想删除William Hart from Joan我怎么会这么做呢?

要更新/删除,您需要增加/减少left/right分支所有元素的值。
您可以找到的查询示例here.

效率如何我不知道。

嵌套集在更新/插入/删除时与大树一起工作非常缓慢。而且选择速度非常快。
因此,仅将此模型用于静态数据,这些数据在大多数情况下将不进行更改地存储,并且该树不会包含数千个节点(或者任何更新都需要几分钟才能完成)。物化路径的工作速度要快得多。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

处理mysql中的嵌套集? 的相关文章

  • ER_ACCESS_DENIED_ERROR:用户 ''@'localhost' 的访问被拒绝(使用密码:NO)

    我有这个问题 我已经研究过但无法解决它 我想它与数据库权限有关 但我无法修复它 if error throw error Error ER ACCESS DENIED ERROR Access denied for user localho
  • 如何通过 API 平台使用“paramconverter”?

    如何通过 Symfony API 平台实现或使用 paramconverter 我想在路线上使用实体 ID 并立即生成一个对象 准备在控制器中使用 我没有在这个项目上使用注释 路由配置位于 YAML 文件中 resources App Me
  • Rails 多租户架构,限制多个租户的访问范围

    目前我们有一个单租户数据库架构 MySQL 运行着超过 100 个数据库 我们使用 Apartment gem 切换子域上的数据库连接 一切都很顺利 然而 我们现在需要创建所谓的 伞 客户端 它可以访问一组现有客户端的所有数据 我不认为这对
  • PHP 中的异步数据库/服务调用:Gearman 与 pthreads

    在我们的 LAMP 站点上 我们遇到一些服务必须多次调用数据库才能提取数据的问题 通常在 PHP 中完成此操作的方式 至少我的经验 是串行的 这显然是低效的 我们可以通过使用缓存和聚合一些查询来缓解一些低效率的问题 但在某些情况下我们仍然需
  • 如何在MySQL中查找上周的数据

    我想显示来自 Q1 每个学生只有最后一周 Q2 每个学生只有最后一个月 我怎样才能实现这个目标 一周演示 http sqlfiddle com 2 f1fbb 3 当月演示 http sqlfiddle com 2 f1fbb 4 CREA
  • 使用 mysql2 gem 获取最后插入的 id

    我有这样的代码 require mysql2 db query insert into clients Name values client 我可以通过 1 个查询返回最后插入的 ID 吗 您可以使用last id客户端实例的方法 clie
  • Kohana_Exception [ 0 ]:需要有效的 cookie salt。请设置 Cookie::$salt

    我正在学习本教程 http kowsercse com 2011 09 04 kohana tutorial beginners http kowsercse com 2011 09 04 kohana tutorial beginners
  • MySql 查询在选择中将 NULL 替换为空字符串

    如何用空字符串替换 select 中的 NULL 值 输出 NULL 值看起来不太专业 这是非常不寻常的 根据我的语法 我希望它能够工作 我希望能得到一个解释 为什么没有 select CASE prereq WHEN prereq IS
  • php在html页面中创建额外空间

    我是网络开发新手 我真的被这个愚蠢的问题困扰了 当我在 html 代码之前插入 php 代码时 如下所示 它在我的页面顶部创建了额外的空白空间 并将整个内容 推下 是否有可能以某种方式避免创建额外的空间 如果 php 代码位于 html 的
  • 在 PHP 中使用 file_get_contents 进行 PUT 请求的错误请求

    这个 api 调用使用 Postman REST 客户端 可以正常工作 但是当我的 GAE 应用程序中的服务器上发出请求时 我当前收到以下错误 HTTP 请求失败 在 C Projects app file php 第 26 行 打开流失败
  • 自动将所有mysql表转储到单独的文件中?

    我想将每个 mysql 表转储到单独的文件中 手册指出其语法是 mysqldump options db name tbl name 这表明您事先知道表名称 我现在可以设置知道每个表名称的脚本 但是假设我在路上添加了一个新表并且忘记更新转储
  • PHP Netbeans:xdebug 在每个 include() 或 require() 上停止

    我刚刚发现使用 netbeans IDE 中集成的 xdebug 进行 PHP 调试 我认为这很棒 没有它我怎么生活 但有一个问题 如果我在代码深处设置了一个断点 我必须在到达断点之前多次按 继续 F5 因为脚本会在每个 include 和
  • 是否可以使用 Javascript 读取 PHP 会话?

    我正在使用 cakePHP 1 26 在控制器中 我得到了一个包含以下代码行的函数 this gt Session gt write testing user this gt Session gt read testing 现在系统编写了一
  • PHP 和 ADODB 连接失败

    我的任务是迁移服务器 这包括移动我没有构建的应用程序 其中一些具有 ADODB connection 我似乎无法在新服务器上修复它 我只得到空白屏幕 我已经对 ADODB connection 与 PHP 进行了相当广泛的研究 但找不到明确
  • zend 模块特定配置问题

    我使用 zend 框架构建 REST Web 服务 并且使用模块来分隔我的 api 版本 现在 我想为每个模块 v1 和 v2 都有一个单独的配置文件 主要用于指定单独的数据库连接 我有这样的目录结构 application modules
  • 何时以及为何应使用 $_REQUEST 而不是 $_GET / $_POST / $_COOKIE?

    标题中的问题 当所有 3 个都发生时会发生什么 GET foo POST foo and COOKIE foo exist 其中哪一个被包含到 REQUEST 我想说永远不会 如果我想通过各种方法设置某些内容 我会为每个方法编写代码以提醒自
  • 选择MySql表数据放入数组中

    我尝试从 mysql 捕获数据并将它们全部放入数组中 认为 users table id name code 1 gorge 2132 2 flix ksd02 3 jasmen skaod2 sql mysql query select
  • 在 MySQL 中分割逗号分隔值

    我正在尝试将字符串中以逗号分隔的 值拆分为多列 样本数据 COL1 COL2 COL3 000002 000003 000042 09 31 51 007 004 007 预期输出 Pno Cno Sno 000002 09 007 000
  • Laravel 集合 .each() + array_push

    需要有关 Laravel 上 each 方法内的 array push 的帮助 我无法在此代码上获取容器数组 imagesData array collect data images gt each function v k use ima
  • 如何在 Laravel 中创建一条包罗万象的路线

    我需要一个 Laravelroutes php将捕获所有流量到特定的条目example com premium section网站 以便我可以提示人们在访问优质内容之前成为会员 您还可以通过在参数上使用正则表达式来捕获 全部 Route g

随机推荐