MySQL ON DUPLICATE KEY 插入审计或日志表
有办法做到这一点吗?
INSERT IGNORE INTO some_table (one,two,three) VALUES(1,2,3)
ON DUPLICATE KEY (INSERT INTO audit_table VALUES(NOW(),'Duplicate key ignored')
我真的不想为此使用 PHP :(
谢谢!
Is there a way to accomplish this?
INSERT IGNORE INTO some_table (one,two,three) VALUES(1,2,3)
ON DUPLICATE KEY (INSERT INTO audit_table VALUES(NOW(),'Duplicate key ignored')
I really don't want to use PHP for this :(
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想考虑使用存储过程,可以使用 <代码>声明继续处理程序。下面是一个示例:
让我们添加一些数据,尝试插入重复的键:
结果:
如果审核在数据库级别很重要,您可能只想授予
EXECUTE
权限,以便数据库用户只能调用存储过程。If you want to consider using a stored procedure, you can use a
DECLARE CONTINUE HANDLER
. Here's an example:Let's add some data, trying to insert a duplicate key:
Result:
If auditing is important on a database level, you may want to grant
EXECUTE
permissions only so that your database users can only call stored procedures.ON DUPLICATE KEY 用于更新行,而不是插入到另一个表中,您必须根据第一个结果使用两个查询。
编辑:您也可以使用触发器
ON DUPLICATE KEY is used to update the row, not to insert in another table, you have to use two queries according the first's result.
EDIT : you can use a trigger too
不确定这是否有效,但请查看 MySQL
伪代码的 IF 语句
Not sure if this would work but look into IF statement for MySQL
Pseudo-code