sql 组织结构插入时的MYSQL触发器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 组织结构插入时的MYSQL触发器相关的知识,希望对你有一定的参考价值。

DELIMITER |
CREATE TRIGGER insert_organization_data
AFTER INSERT ON organization_structures
FOR EACH ROW BEGIN
    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path)
    VALUES (NEW.id, NEW.id, 0, CONCAT(NEW.name,"/"));

    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path)
        SELECT parent_id, NEW.id, depth + 1, CONCAT(path, NEW.name, "/")
        FROM organization_structure_trees
        WHERE child_id = NEW.parent_id;
END; |
DELIMITER ;

BEGIN

    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path, sequence_path)
    VALUES (NEW.id, NEW.id, 0, CONCAT(NEW.name,"/"), CONCAT(LPAD(CONCAT(NEW.id,'00'),12,'0'),"/ ")); 

    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path, sequence_path)
        SELECT parent_id, NEW.id, depth + 1, CONCAT(path, NEW.name, "/"), CONCAT(sequence_path, CONCAT(LPAD(CONCAT(NEW.id,'00'),12,'0'),"/ "))
        FROM organization_structure_trees
        WHERE child_id = NEW.parent_id;
END

以上是关于sql 组织结构插入时的MYSQL触发器的主要内容,如果未能解决你的问题,请参考以下文章