mysql语法问题
我正在尝试在 MySQL 上创建触发器,但遇到语法问题,但我无法找到该问题。如果有更多经验的人可以帮助我,那就太好了(这是我第一次使用 MySQL!)...
我创建这个触发器的原因是为了删除所有孤立的“标签”,它有很多-与“service_descriptor”的多对关系(这两个实体通过 service_labels 链接)。
我的代码是:
CREATE TRIGGER `trg_delete_orphan_label` AFTER DELETE
FOR EACH ROW ON `restdb`.`service_labels`
DELETE FROM `restdb`.`labels`
WHERE EXISTS (SELECT *
FROM old D
LEFT_JOIN `restdb`.`service_labels` SL ON SL.`id_label` = D.`id_label`
AND D.`id_service` = SL.`id_service`
WHERE SL.`id_label` IS NULL
`restdb`.`labels`.`id` = D.SL.`id_label`);
提前致谢!
I´m trying to create a trigger on MySQL but I´m having a syntax problem, which I was not able to find. If someone more experience could help me it would be great (it´s the first time I use MySQL!)...
The reason why I´m creating this trigger is for deleting all the orphan "labels", which has a many-to-many relation with "service_descriptor" (this two entities are linked by service_labels).
The code I have is:
CREATE TRIGGER `trg_delete_orphan_label` AFTER DELETE
FOR EACH ROW ON `restdb`.`service_labels`
DELETE FROM `restdb`.`labels`
WHERE EXISTS (SELECT *
FROM old D
LEFT_JOIN `restdb`.`service_labels` SL ON SL.`id_label` = D.`id_label`
AND D.`id_service` = SL.`id_service`
WHERE SL.`id_label` IS NULL
`restdb`.`labels`.`id` = D.SL.`id_label`);
Thanks in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经错过并
尝试此代码
You have missed AND
Try this code
您的触发器存在一些问题,最明显的是“FOR EACH ROW”的位置,以及您将 OLD 视为表格的方式,而实际上它只是一行。
这应该适合你:
There are a couple of problems with your trigger, most notably the placement of "FOR EACH ROW", and the way you are treating OLD like a table, when it's really just a row.
This should work for you:
谢谢大家...由于您的帮助,我终于解决了这个问题...
最后的工作是:
在
restdb
上删除后创建触发器trg_delete_orphan_label
。服务描述符
对于每一行
从
restdb
删除。标签
其中
id
不在 (选择
restdb
.service_labels
.id_label
FROM
restdb
.service_labels
);Thanks everyone ... I´ve finaly solved it because of your help...
In the end the work is:
CREATE TRIGGER
trg_delete_orphan_label
AFTER DELETE ONrestdb
.service_descriptor
FOR EACH ROW
DELETE FROM
restdb
.labels
WHERE
id
NOT IN (SELECT
restdb
.service_labels
.id_label
FROM
restdb
.service_labels
);