MySQL /多删除语句-#无法删除或更新父行

时间:2018-07-06 06:16:59

标签: mysql phpmyadmin

我尝试创建过程以删除类别和类别之间的联系,但不幸的是,我不知道如何处理我的关系。

<blockquote>
      <span>I don’t even bring up technology.</span> 
          I talk about the flow of data.&rdquo;
      <cite>&ndash;Rick Hassman, CIO, Pella</cite>
</blockquote>

这是我创建的代码:

    <blockquote>
            I don’t even bring up technology.
              I talk about the flow of data.&rdquo;
          <cite>&ndash;Rick Hassman, CIO, Pella</cite>
    </blockquote>

表格:

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`saver`.`categorytree`, CONSTRAINT `categorytree_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`))

For select. 有人可以帮忙吗?

编辑:

delete con, cc from categorytree con join categorytree ct on con.child_id = ct.child_id join category cc on con.child_id = cc.id where con.parent_id = 1 为我工作。谢谢。

create table category (
id int not null AUTO_INCREMENT,
name varchar (50) not null,
primary key(id) 
);


create table categorytree(
parent_id int not null,
child_id int not null ,
depth int,
FOREIGN KEY (parent_id) references category(id),
FOREIGN KEY (child_id) references category(id)
);

1 个答案:

答案 0 :(得分:0)

您使用递归函数进行删除或更新

// Function to build hierarchy tree
function hierarchy($array, $parent = 0, $level = 0) {
    $tree = '<ul>';
    foreach ($array as $v) {
        if ($v['parent'] === NULL) $v['parent'] = 0;
        if ($v['parent'] === $parent) {
            $tree .= '<li>';
            $tree .= $v['name'];
            $tree .= hierarchy($array, $v['id'], $level+1);
            $tree .= '</li>';
        }
    }
    $tree .= '</ul>';
    return $tree;
}

链接:https://www.sitepoint.com/community/t/building-multi-level-menu-dynamically-with-pdo-in-php/298325/2