mysql 删除前获取字段值

发布于 2024-11-01 09:47:29 字数 259 浏览 3 评论 0原文

我有问题! 我有一个附件表。我将文件名存储在该表中。我的类别表和附件有关系,如果类别删除,附件上的相关记录也会删除,但我想在删除附件之前获取文件名!

我应该怎么办 ?

DELETE c, a category c 
  JOIN attachment a ON c.id = a.extId 
 WHERE c.lft BETWEEN @left AND @right

我想在顶部查询中删除文件名值(附件字段)之前获取它

I have a problem!
I have an attachment table. I store file name in this table. My category table and attachment have relation together, if a category delete, related records on attachment removed too but I want to get file name from attachment before delete it!

What should I do ?

DELETE c, a category c 
  JOIN attachment a ON c.id = a.extId 
 WHERE c.lft BETWEEN @left AND @right

I want to get file name value ( an attachment field ) before delete it in top query

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

り繁华旳梦境 2024-11-08 09:47:29

创建触发器

DELIMITER $

CREATE TRIGGER ad_attachment_each AFTER DELETE ON attachment FOR EACH ROW
BEGIN
  INSERT INTO deleted_attachments (id, attachment_id, filename, timestamp) 
    VALUES (null, old.id, old.filename, NOW());
END$

DELIMITER ;

此触发器将在每次删除后针对从附件中删除的每一行触发,并将一行添加到表“deleted_attachments” 。

任何操作都有 BEFOREAFTER 触发器。
该操作可以是DELETEINSERTUPDATE
可以使用old虚拟表访问更改之前的值。可以使用new虚拟表访问更改后的值。
这里我使用了old值,因为delete显然没有new值。

有关触发器的更多信息,请参阅:
http://dev.mysql.com/doc/refman/5.1/en /triggers.html

或在 stackoverflow 中搜索 mysql 触发器

Create a trigger

DELIMITER $

CREATE TRIGGER ad_attachment_each AFTER DELETE ON attachment FOR EACH ROW
BEGIN
  INSERT INTO deleted_attachments (id, attachment_id, filename, timestamp) 
    VALUES (null, old.id, old.filename, NOW());
END$

DELIMITER ;

This trigger will fire after every delete for each row that gets deleted from attachment and will add a row to a table "deleted_attachments".

There are triggers BEFORE and AFTER any action.
The action can be DELETE, INSERT, UPDATE.
The values before the change can be accessed by using the old virtual table. The values after the change can be accessed by using the new virtual table.
Here I used the old values, because delete obviously does not have new values.

For more about triggers see:
http://dev.mysql.com/doc/refman/5.1/en/triggers.html

Or search stackoverflow for mysql triggers.

ま柒月 2024-11-08 09:47:29

执行两个查询,第一个是具有相同 joinwhereselect 查询,第二个是 delete 查询:

SELECT attachment.filename 
FROM attachment AS a 
JOIN category AS c ON c.id = a.extId 
WHERE c.lft BETWEEN @left AND @right

Execute two queries, the first one a select with the same join and where and the second your delete query:

SELECT attachment.filename 
FROM attachment AS a 
JOIN category AS c ON c.id = a.extId 
WHERE c.lft BETWEEN @left AND @right
红墙和绿瓦 2024-11-08 09:47:29

删除查询不返回值,因此我建议您事先执行一个简单的 select 语句来获取文件名。然后继续执行删除语句。

Delete queries do not return values, so I suggest you perform a simple select statement beforehand to get the filename. Then proceed with the delete statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文