我是使用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
拜托,我做错了什么?
Tks。
最佳答案
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。