mysql 删除前获取字段值
我有问题! 我有一个附件表。我将文件名存储在该表中。我的类别表和附件有关系,如果类别删除,附件上的相关记录也会删除,但我想在删除附件之前获取文件名!
我应该怎么办 ?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建触发器
此触发器将在每次
删除
后针对从附件中删除的每一行触发,并将一行添加到表“deleted_attachments” 。任何操作都有
BEFORE
和AFTER
触发器。该操作可以是
DELETE
、INSERT
、UPDATE
。可以使用
old
虚拟表访问更改之前的值。可以使用new
虚拟表访问更改后的值。这里我使用了
old
值,因为delete
显然没有new
值。有关触发器的更多信息,请参阅:
http://dev.mysql.com/doc/refman/5.1/en /triggers.html
或在 stackoverflow 中搜索
mysql 触发器
。Create a trigger
This trigger will fire
after
everydelete
for each row
that gets deleted from attachment and will add a row to a table "deleted_attachments".There are triggers
BEFORE
andAFTER
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 thenew
virtual table.Here I used the
old
values, becausedelete
obviously does not havenew
values.For more about triggers see:
http://dev.mysql.com/doc/refman/5.1/en/triggers.html
Or search stackoverflow for
mysql triggers
.执行两个查询,第一个是具有相同
join
和where
的select
查询,第二个是delete
查询:Execute two queries, the first one a
select
with the samejoin
andwhere
and the second yourdelete
query:删除查询不返回值,因此我建议您事先执行一个简单的 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.