如何在parent_id => id结构中删除树

时间:2013-01-27 22:00:56

标签: php mysql recursion tree

我有一个数据库表(目录),使用parent_id=>id结构将树映射出来。

我有另一个表(文件)将文件映射到第一个表directory_id => directories.id

中的项目

我希望能够做的是,给定第一个表中的id,删除第二个表中与所选id在同一树中相同或更高的所有行。

然后删除第一个表中同一树中相等或更高的所有行。

解释它的最好方法是文件系统,删除目录,删除它下面的所有内容。

我能想到的唯一方法是(半伪代码!):

$id = :id
$files = array();
$directories = array($id);
delete_tree($query:results, $files, $directories);

function delete_tree($id, &$files, &$directories){
    $query = SELECT `id` FROM `directories` WHERE `parent_id` = $id;
    foreach($query:results as $directory){
        $directories[] = $directory:id;
        $query2 = SELECT `id` FROM `files` WHERE `directory_id` = $directory:id;
        foreach($query2:results as $file){
            $files[] = $file:id
        }
        delete_tree($directory:id, $files, $directories);
    }
}

$query = DELETE from `directories` WHERE `id` IN (explode(',', $directories));
$query = DELETE from `files` WHERE `id` IN (explode(',', $files));

2 个答案:

答案 0 :(得分:1)

你可以尝试使用递归的sql; - )

在这里你可以找到信息:

http://wiki.yaslaw.info/wikka/MySQLTree

在“4)..”下,您可以找到所需的sql条件示例。

示例代码:

SELECT
    nav.id,
    nav.title
FROM
    (
        SELECT
            @rownum := @rownum+1 AS rownum, 
            IF(@lastid <> mylist.id, @id := mylist.id, @id) AS pathid,
            @lastid := mylist.id AS id,
            @id := (SELECT parentID FROM nav  WHERE id = @id) AS parentID
        FROM
            (SELECT @id := 0, @lastid := 0, @rownum := 0) AS vars,
            (SELECT id FROM nav) AS myloop,
            (SELECT id FROM nav) AS mylist
    ) AS t
    INNER JOIN nav
        ON t.id = nav.id
WHERE
    pathid = 2  // all subitems from id=2

答案 1 :(得分:1)

我会通过数据库约束来实现。

假设我有以下表格

enter image description here

我可以定义两个关系,以便通过删除主记录在级联中删除记录。

phpMyAdmin中的示例:

//目录

enter image description here

//文件

enter image description here

现在我删除目录“03”,id = 10

//目录

enter image description here

//文件

enter image description here

如您所见,两个表中的所有记录都是节点的子节点。很快,没有任何脚本。

这是一个小转储:

CREATE TABLE IF NOT EXISTS `directories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `parent_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=cp1250 AUTO_INCREMENT=13 ;

INSERT INTO `directories` (`id`, `name`, `parent_id`) VALUES (0, 'root', 0),(6, '02', 0), (10, '03', 0),(11, '031', 10),(12, '032', 10);

CREATE TABLE IF NOT EXISTS `files` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `directory_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `directory_id` (`directory_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=cp1250 AUTO_INCREMENT=7 ;


INSERT INTO `files` (`id`, `name`, `directory_id`) VALUES (2, '031_f01', 11),(3, '031_f02', 11),(4, '02_f01', 6),(6, '02_f02', 6);


ALTER TABLE `directories`
  ADD CONSTRAINT `directories_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `directories` (`id`) ON DELETE CASCADE;

ALTER TABLE `files`
  ADD CONSTRAINT `files_ibfk_1` FOREIGN KEY (`directory_id`) REFERENCES `directories` (`id`) ON DELETE CASCADE;