MySQL:更新触发器。如果 UPDATE 的 where 子句无法匹配任何行,则获取该列的值?
MySQL:在更新触发器的主体中,如果 where
e 子句不匹配任何行,我能否获取触发查询的 where
子句中指定的列的值根本吗?
我必须执行以下操作,但不使用直接查询,例如 ON DUPLICATE KEY UPDATE 等:
如果我有:
UPDATE my_table SET idiotism_level=5 WHERE name='Pencho'
... 并且 where 子句不匹配任何行,我想自动触发插入具有 < code>name='Pencho' 在更新之前,然后 UPDATE
可能会匹配并正常工作。
是否可以 ?
MySQL: In update trigger's body, can I obtain the value of a column that is specified in the where
clause of the triggering query if the wher
e clause does not match any rows at all?
I have to do the following, but NOT USING direct query such as ON DUPLICATE KEY UPDATE so on:
If I have:
UPDATE my_table SET idiotism_level=5 WHERE name='Pencho'
... and the where clause match NO ROWS, I'd want to automatically trigger an insertion of a row having name='Pencho'
before the update, and then the UPDATE
would presumably match, and work properly.
Is it possible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可以在其他数据库系统 (PostgreSQL) 中的规则中进行,但 MySQL 中不存在。这是规则而不是触发器,因为您应该分析查询而不是查询结果。
但对于 MySQL,您可以使用 MySQL-Proxy。您应该能够更改您的更新查询并构建插入,通过从 MySQL 代理运行一些“检查行存在”额外查询(我并不是说这是一个很好的解决方案,但如果您没有办法使为了更好地执行代码,您可以在此级别修复它)。
This could be make in a RULE in other database systems (PostgreSQL), that does not exists in MySQL. It's a Rule and not a trigger as you should analyse the query and not the result of the query.
But for MySQL you can make pre-query jobs by using MySQL-Proxy. You should be able to alter your update query and build an insert, By running some 'check row exists' extra query from the MySQL-Proxy (I'm not saying this is a nice solution, but if you have no way to make the code to act better you can fix it at this level).
不会。更新触发器会针对每更新的行触发一次,而不是针对执行的每个更新命令触发一次。如果没有任何更新,则无法触发触发器。您需要在应用程序中通过检查查询返回的更新行数来处理此问题。
No. An update trigger fires once for each row that gets updated, not once for each update command that's executed. There's no way to make the trigger fire if nothing is updated. You would need to handle this in your application by checking the number of updated rows returned by your query.
如果名称有唯一索引,您可以使用
REPLACE
If name has a unique index on it you can use
REPLACE