我是靠谱客的博主 坚强路灯,最近开发中收集的这篇文章主要介绍mysql通过触发器获取数据表的操作id,MYSQL更新触发器获取更新的行ID,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I have a table in MySQL which I am updating. I would like the trigger to get the updated row ID so as to do some work with it. How can I do it? I can do this in SQL Server, but I am new to MySQL and couldn't find anything online.

UPDATE...

So I can't share the code, but this is basically want I want to do.

Table:

**id** **product** **price**

1, Coke, 2.05 USD

2, Sprite, 2.00 USD

3, 7Up, 2.10 USD

I want to update id 2, "Sprite" price to 2.50 USD. Now I will do this normally, but I need to run a Trigger that will update an Audit table. The Audit table will need to register the id of the product. So basically I need to know that the updated row's id was 2...

is there anything similar in sql?

解决方案

You can refer to the id column using either NEW.id or OLD.id as long as the id column will not change.

Generally spoken, with the alias NEW you can get the current value of the updated column and with OLD you obtain the value that the column had before the update was performed.

CREATE TRIGGER au_product_table

AFTER UPDATE ON product_table

FOR EACH ROW

UPDATE audit

SET product_id = NEW.id

WHERE your_condition;

最后

以上就是坚强路灯为你收集整理的mysql通过触发器获取数据表的操作id,MYSQL更新触发器获取更新的行ID的全部内容,希望文章能够帮你解决mysql通过触发器获取数据表的操作id,MYSQL更新触发器获取更新的行ID所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部