Mysql 如何更新父级和后续子级值

Mysql 如何更新父级和后续子级值,mysql,Mysql,我有一个文件夹表,其中存储目录路径、深度、父ID、标题等 我有这样的文件夹结构 / /services /products again product has some children directories /products/computers and computers has some children directories /products/computers/desktop /products/computers/laptop etc,. 如果您想将/产品作为/服务的子产品,

我有一个文件夹表,其中存储目录路径、深度、父ID、标题等

我有这样的文件夹结构

/
/services
/products
again product has some children directories
/products/computers
and computers has some children directories
/products/computers/desktop
/products/computers/laptop etc,.
如果您想将
/产品
作为
/服务
的子产品,它应该是这样的

/services/products
/services/products/computers
/services/products/computers/desktop
/services/products/computers/desktop etc,.
我尝试了以下查询,我可以更新单个级别,但不能更新其后续子级别

SELECT 
GROUP_CONCAT(f.id),
f.nav_depth
INTO
@ids, @depth
FROM folders f
JOIN (SELECT f1.id,
f1.parent__id 
FROM folders f1
WHERE f1.directory_path REGEXP '^/products') ta ON ta.id = f.id;

UPDATE folders
SET 
directory_path = CONCAT(parentPath,'/',title),
parent__id = 2,
nav_depth = parentnavdepth+1
WHERE id IN (@ids);
表结构

CREATE TABLE `folders` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE   CURRENT_TIMESTAMP,
  `directory_path` tinytext,
  `nav_depth` tinyint(3) DEFAULT '0',
  `parent__id` bigint(20) DEFAULT NULL,
  `resource_id` varchar(36) DEFAULT NULL,
  `title` varchar(128) DEFAULT NULL,
  `user__id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `UK_ltal9g0512nekqfp83u5l1huo` (`parent__id`),
  KEY `UK_cuff3r728xl6ynlp3u1dr5vt9` (`nav_depth`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;

我怀疑你可能想得太多了。您可以按如下方式更新目录路径:

update folders set directory_path = concat('/services', directory_path), nav_depth = nav_depth + 1 
  where directory_path = '/products' 
    or directory_path like '/products/%';
您必须在第二个查询中更新新“/services/products”路径的父\u id

我不知道这会比什么更难

update folders set parent__id = 2
  where directory_path = '/services/products';

请把您的桌子包括在内好吗structure@pala_-我已经用表格结构更新了我的问题。对不起,我应该要求提供样本数据。我知道你把它放在最上面,但如果放在桌子上会更有帮助format@pala_-我在问题中粘贴了Sqlfiddle链接最大的障碍是更新父\uu Id,我尝试了上面的查询,它可以很好地更新目录路径。您需要更新的唯一父\u Id肯定是路径“/products”。所有其他人都不是这条路的父母?我刚刚补充了这一步——在某种程度上它错了吗?