OP的评论:
您之前会怎么做?
您可以找到
auto_increment要分配给新记录的当前值。
并在
before触发器中使用与
user_records表的父用户ID 相同的触发器。
您必须查询
information_schema.tables表以找到该值。
范例 :
use `gknet`;delimiter $$drop trigger if exists before_create_user; $$create definer=`root`@`localhost` trigger `before_create_user` before insert on `users` for each row begin declare fk_parent_user_id int default 0; select auto_increment into fk_parent_user_id from information_schema.tables where table_name = 'users' and table_schema = database(); insert into user_records ( action, userid, timestamp ) values ( 'created', fk_parent_user_id, now() );end;$$delimiter ;
观察结果 :
根据 last_insert_id() 上的mysql文档
****,
“ 如果使用单个INSERT语句插入多行,则仅
LAST_INSERT_ID()返回为第一行插入而生成的值。 ”
因此,批量插入中的依靠
last_insert_id()和
auto_increment字段值似乎不可靠。



