MYSQL TRIGGER子查询更新

时间:2014-05-04 16:14:32

标签: mysql sql phpmyadmin

我正在使用我的SQL,我在SQLSERVER中开发了一个运行完美的触发器,现在我正在尝试将其转换为在Mysql中使用,但是没有工作,我不知道该怎么做。

这里是代码:

CREATE TRIGGER tg_test AFTER UPDATE ON  YET FOR EACH ROW

INSERT INTO CONTROLETRIGGER(idAluno, idNivel)
values(NEW.IdAluno, NEW.idNivel);


UPDATE HistoricoNivel AS E
join(
select max(h.idNivel) Nivel, h.idAluno
from HistoricoNivel h
join CONTROLETRIGGER v on h.idaluno = v.idaluno
WHERE h.dtFim is null
group by h.idAluno
)X on E.idAluno = X.idAluno and E.idNivel= X.Nivel
join CONTROLETRIGGER vi on vi.idaluno = E.idAluno
set E.dtFim = case when X.idnivel <> vi.idNivel then CURRENT_DATE() else null end

请问,我做错了什么?

韩国社交协会

1 个答案:

答案 0 :(得分:0)

X子查询没有名为idnivel的字段,因此case语句不会编译。

我假设你想要的是nivel,这将完全触发;

CREATE TRIGGER tg_test AFTER UPDATE ON  YET FOR EACH ROW
 INSERT INTO CONTROLETRIGGER(idAluno, idNivel) VALUES (NEW.IdAluno, NEW.idNivel);
 UPDATE HistoricoNivel AS E
 JOIN(
  SELECT MAX(h.idNivel) Nivel, h.idAluno
  FROM HistoricoNivel h
  JOIN CONTROLETRIGGER v ON h.idaluno = v.idaluno
  WHERE h.dtFim IS NULL
  GROUP BY h.idAluno
 ) X 
 ON E.idAluno = X.idAluno AND E.idNivel= X.Nivel
 JOIN CONTROLETRIGGER vi ON vi.idaluno = E.idAluno
 SET E.dtFim = CASE WHEN X.nivel <> vi.idNivel THEN CURRENT_DATE() ELSE NULL END
//

See it compile here