【问题标题】:Nested set INSERT operation for SQLiteSQLite 的嵌套集 INSERT 操作
【发布时间】:2011-09-30 18:16:04
【问题描述】:

我正在尝试为 sqlite 数据库实现嵌套集模型。 到目前为止,我已经实现了获取叶节点、查找下属等。 我使用this 教程作为参考 但是,我被困在插入一个新节点上。 这是网站上的代码

LOCK TABLE nested_category WRITE;

SELECT @myRight := rgt FROM nested_category
WHERE name = 'TELEVISIONS';

UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE nested_category SET lft = lft + 2 WHERE lft > @myRight;

INSERT INTO nested_category(name, lft, rgt) VALUES('GAME CONSOLES', @myRight + 1, @myRight + 2);

UNLOCK TABLES;

我知道sqlite中没有锁表。我的问题是我需要将第一个查询的结果传递给下一个查询。这里是通过@使用用户创建的变量来完成的。我不知道如何在 SQLite 中做到这一点。

提前致谢。

【问题讨论】:

    标签: sql sqlite tree nested-sets


    【解决方案1】:

    一个想法是将变量myRight 替换为生成它的查询。 也就是将代码移动到纯SQL中。

    UPDATE nested_category SET rgt = rgt + 2 
    WHERE rgt > (SELECT rgt 
                 FROM nested_category
                 WHERE name = 'TELEVISIONS');
    
    UPDATE nested_category SET lft = lft + 2 
    WHERE lft > (SELECT rgt 
                 FROM nested_category
                 WHERE name = 'TELEVISIONS');
    
    INSERT INTO nested_category(name, lft, rgt)
    SELECT 'GAME CONSOLES', rgt + 1, rgt + 2 
    FROM nested_category
    WHERE name = 'TELEVISIONS' ;
    

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 2012-07-18
      • 2019-11-28
      • 2020-01-08
      • 2014-01-11
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多