我正在尝试创建一个触发器,它将根据另一个表中的select
从一个表中删除行表格operatorpositions
列operator
,line
,pos
表格positiontags
列line
,position
,tag
表格operatortags
列operator
,tag
我的触发器看起来像这样
CREATE TRIGGER removeOperatorPosition AFTER DELETE ON operatorpositions
FOR EACH ROW
BEGIN
DELETE FROM operatortags WHERE gen = NEW.operator
AND tag = (SELECT tag FROM positiontags WHERE position = NEW.pos AND line = NEW.line);
END;
问题是一个位置可能有多个标记,因此子查询将返回多行。我该如何更改它以便它可以使用多个标签?
答案 0 :(得分:3)
如果我理解你,你想要删除所有标签
所以尝试在子查询之前用IN
CREATE TRIGGER removeOperatorPosition AFTER DELETE ON operatorpositions
FOR EACH ROW
BEGIN
DELETE FROM operatortags WHERE gen = NEW.operator
AND tag IN (SELECT tag FROM positiontags WHERE position = NEW.pos AND line = NEW.line);
END;
答案 1 :(得分:3)
CREATE TRIGGER removeoperatorposition AFTER DELETE
ON operatorpositions
FOR EACH ROW
BEGIN
DELETE FROM operatortags
WHERE gen = new.operator
AND tag IN (SELECT tag
FROM positiontags
WHERE position = new.pos
AND line = new.line);
END;
从=
更改为IN
运算符应该可以处理返回多行的子查询
答案 2 :(得分:3)
更有效的方法是使用连接 -
CREATE TRIGGER removeOperatorPosition AFTER DELETE ON operatorpositions
FOR EACH ROW
BEGIN
DELETE ot
FROM operatortags ot
INNER JOIN positiontags pt
ON ot.tag = pt.tag
WHERE ot.gen = OLD.operator
AND pt.position = OLD.pos
AND pt.line = OLD.line;
END;