dousha1831 2014-09-17 16:54
浏览 39
已采纳

BEFORE UPDATE触发器是否影响mysqli_insert_id结果?

I have a mysql table which has a trigger attached, that logs changes in this table to a second one

CREATE TRIGGER log_table BEFORE UPDATE ON table1
  FOR EACH ROW BEGIN
    INSERT INTO log_table(filed) VALUES(NEW.field);
  END;
//

Now if I perform an INSERT INTO table1 from PHP an call mysqli_insert_id() afterwards.

Would that return the new ID in table1? Or the new ID in log_table?

  • 写回答

1条回答 默认 最新

  • dongwei2882 2014-09-17 19:48
    关注

    LAST_INSERT_ID() called after the INSERT statement will return the new ID in table1 (of the INSERT statement), not the one in the log_table (of the trigger).

    This is documented in the manual section of LAST_INSERT_ID:

    Within the body of a stored routine (procedure or function) or a trigger, the value of LAST_INSERT_ID() changes the same way as for statements executed outside the body of these kinds of objects. The effect of a stored routine or trigger upon the value of LAST_INSERT_ID() that is seen by following statements depends on the kind of routine:

    • If a stored procedure executes statements that change the value of LAST_INSERT_ID(), the changed value is seen by statements that follow the procedure call.

    • For stored functions and triggers that change the value, the value is restored when the function or trigger ends, so following statements will not see a changed value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示