我正在使用MySQL。我必须创建一个包含多个插入语句的存储过程。其中第二个插入语句需要第一个插入的行 ID。 第三个插入语句需要第二个插入的行 ID。下面是我的SQL语句,
INSERT INTO table1( ccc ) VALUES ( 'a' );
SET scopeIdentity = LAST_INSERT_ID();
INSERT INTO table2( ccc,tttId ) VALUES ( 'b',scopeIdentity );
SET SET scopeIdentity2 = LAST_INSERT_ID();
INSERT INTO table3( ccc,tttId2 ) VALUES ( 'b',scopeIdentity2 );
欢迎任何建议!
提前致谢 桑吉塔
最佳答案
你可以这样尝试:
INSERT INTO table1( ccc ) VALUES ( 'a' );
INSERT INTO table2( ccc,tttId ) VALUES ( 'b',LAST_INSERT_ID());
INSERT INTO table3( ccc,tttId2 ) VALUES ( 'b',LAST_INSERT_ID());
即,您不必设置 LAST_INSERT_ID() 的值变量中,然后使用该变量插入值。
- INSERT statements that store a value into an AUTO_INCREMENT column. This is true whether the value is automatically generated by storing
the special values NULL or 0 into the column, or is an explicit
nonspecial value.- In the case of a multiple-row INSERT statement, the return value of mysql_insert_id() depends on the MySQL server version.
关于mysql - 从 MySQL 获取范围标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793806/