这个表的表名字是: HostGroups
需求是:当向 HostGroups 表中插入一条数据的时候,根据插入数据的 HostGroupFlag 的值找到和他相等的 HostGroupID 值,然后给找到的这条记录的 total 字段+1 ;
![]() | 1 Ashitaka4 OP 逻辑有没有问题。 |
![]() | 2 Ashitaka4 OP 然而 18 分钟过去了。。。 |
3 chinajik 2015-12-04 17:56:23 +08:00 INSERT INTO TABLE (a,c) VALUES (1,3) ON DUPLICATE KEY UPDATE c=c+1; |
![]() | 5 KIDJourney 2015-12-04 21:13:59 +08:00 You can't do that . [document]( http://dev.mysql.com/doc/refman/5.0/en/stored-program-restrictions.html) |
![]() | 6 KIDJourney 2015-12-04 21:15:11 +08:00 Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger. |
![]() | 7 msg7086 2015-12-04 22:01:48 +08:00 ![]() 其实我觉得你问题没说清楚。 如果没有读过《提问的智慧》,请读一下。 本质上来说,你需要提供一个简单的表结构,一个插入语句,和你预期的行为方式。 |
![]() | 8 Ashitaka4 OP @msg7086 多谢提醒,上次比较着急,而且对其需求也不甚清楚,特来补充。 一张有 4 个字段的表 HostGroups , NAME Varchar(10), HostGroupFlag INT , HostGroupID INT , Total INT 。 执行: insert into HostGroups values(shandong,11,12,13); commit ; 当插入执行后,将此条语句的 HostGroupFlag 字段也就是 12 这个值 和表中其他数据行的 HostGroupID 字段里值进行对比查找,如果一致, update 所找到的那条数据的 Total 字段值+1 。 不知道我这么表达您能不能明白,请教怎么实现这种需求。先谢谢了。 |
![]() | 9 msg7086 2015-12-07 13:38:14 +08:00 @Ashitaka4 似乎是看明白了。 你的期望是跟着 insert 以后再做一次 update hostgroups set total = total + 1 where hostgroupid = 12 是吗? |
![]() | 11 KIDJourney 2015-12-10 19:03:35 +08:00 我回答你看不懂吗。。。 在 mysql 里面你不能这样做。。。 触发器不能修改正在被使用的表。。。 我还把文档发给你了,你都不看的吗。。。 |
![]() | 12 KIDJourney 2015-12-10 21:25:26 +08:00 |