MySQL触发器插入后使用从不同表中选择查询

Har*_*ane 4 mysql triggers insert

DBA的新人感谢与我的关系.

概述: 我有组,子组和用户.

  1. 用户可以是Group的所有者,因此应该是其所有子组的所有者
  2. 用户可以是组的协作者或关注者,因此应该是其所有子组的协作者或关注者
  3. 用户可以是只是子组的协作者或追随者

表格如下(简化):

(topic_id,标题)

子组(subtopic_id,title,topic_id)

rel_Group(user_id,topic_id,type)//确定用户与组(所有者,协作者或关注者)之间的关系

rel_Subgroup(user_id,subtopic_id,type)//确定用户与子组(所有者,协作者或关注者)的关系

用户(user_id)

我想在创建子组时创建一个触发器,该子组将在rel_Subgroup中INSERT/UPDATE/DELETE行,因此作为所有者,协作者或组的跟随者的用户分别是子组的所有者,协作者或跟随者

这是我得到的最接近但仍然得到的: #1415 - 不允许从触发器返回结果集.

SQL查询

delimiter //
create trigger Transfer_Rights_to_Subgroup
after insert 
on Subgroup
for each row
begin
select user_id,type from rel_Group where rel_Group.topic_id = NEW.topic_id;
insert into rel_Subgroup VALUES (rel_Group.user_id,NEW.subtopic_id,rel_Group.type); 
END; //
delimiter ;
Run Code Online (Sandbox Code Playgroud)

我希望对插入进行排序,然后找出更新/删除.

任何帮助,非常感谢!

谢谢

Har*_*ane 11

管理解决它:

DROP TRIGGER IF EXISTS Transfer_Rights_to_Subgroup;
DELIMITER //
CREATE TRIGGER Transfer_Rights_to_Subgroup AFTER INSERT ON subgroup
FOR EACH ROW
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE c1 INT;
    DECLARE c2 INT;
    DECLARE cur CURSOR FOR SELECT User_ID,Type FROM rel_group WHERE rel_group.Topic_ID =     NEW.Topic_ID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur;
        ins_loop: LOOP
            FETCH cur INTO c1,c2;
            IF done THEN
                LEAVE ins_loop;
            END IF;
            INSERT INTO rel_Subgroup VALUES (c1,NEW.Subtopic_ID,c2);
        END LOOP;
    CLOSE cur;
END; //
DELIMITER ;
Run Code Online (Sandbox Code Playgroud)