我正在使用我的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
请问,我做错了什么?
韩国社交协会
答案 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
//