我有一个文件夹表,其中我存储目录路径,深度,parentId,标题等,
我有像这样的文件夹结构
/
/services
/products
again product has some children directories
/products/computers
and computers has some children directories
/products/computers/desktop
/products/computers/laptop etc,.
如果想让/products
成为/services
的孩子,它应该会变成这样
/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;
答案 0 :(得分:0)
我怀疑你可能一直在思考它。您可以像这样更新目录路径:
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'路径的parent_id。
我不知道这会比
更难update folders set parent__id = 2
where directory_path = '/services/products';