0

更新问题表后,我需要从响应表中删除行,但出现此错误:子查询返回多行。有没有办法让它工作?

CREATE TRIGGER delete_responses AFTER UPDATE ON  questions
FOR EACH ROW 
BEGIN 
IF NEW.active != OLD.active 
THEN DELETE FROM responses WHERE option_id = (
SELECT option_id
FROM options
WHERE question_id = OLD.question_id);
4

4 回答 4

1

是的,请JOIN改用:

DELETE r
FROM responses AS r
INNER JOIN options AS o ON r.option_id = o.option_id
WHERE o.question_id = OLD.question_id;
于 2013-05-14T11:48:17.173 回答
0

更改== ANYin

CREATE TRIGGER delete_responses AFTER UPDATE ON  questions
FOR EACH ROW 
BEGIN 
IF NEW.active != OLD.active 
THEN DELETE FROM responses WHERE option_id in (
SELECT option_id
FROM options
WHERE question_id = OLD.question_id);
于 2013-05-14T11:48:13.390 回答
0

用于”:

CREATE TRIGGER delete_responses AFTER UPDATE ON  questions
FOR EACH ROW 
BEGIN 
IF NEW.active != OLD.active 
THEN DELETE FROM responses WHERE option_id in (
SELECT option_id
FROM options
WHERE question_id = OLD.question_id);

或限制您的子查询,使其仅返回一行。

于 2013-05-14T11:48:15.143 回答
0

您可以通过 limit 或 distinct 将子查询的结果限制为 1,或者如果您想删除需要使用的子查询中的所有已创建结果WHERE id IN (....)

于 2013-05-14T11:50:12.770 回答