diff --git a/executor/insert_common.go b/executor/insert_common.go index 6ac8109f8aafb..73b7b5e9dcf93 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -494,8 +494,9 @@ func (e *InsertValues) adjustAutoIncrementDatum(d types.Datum, hasValue bool, c if e.filterErr(err) != nil { return types.Datum{}, errors.Trace(err) } - // It's compatible with mysql. So it sets last insert id to the first row. - if e.rowCount == 1 { + // It's compatible with mysql setting the first allocated autoID to lastInsertID. + // Cause autoID may be specified by user, judge only the first row is not suitable. + if e.lastInsertID == 0 { e.lastInsertID = uint64(recordID) } } diff --git a/executor/insert_test.go b/executor/insert_test.go index 1970fdf6967f0..d1114178c7e0c 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -278,6 +278,22 @@ func (s *testSuite) TestInsertWithAutoidSchema(c *C) { `select * from t1 where id = 9`, testkit.Rows(`9 9`), }, + // test last insert id + { + `insert into t1 values(3000, -1), (null, -2)`, + `select * from t1 where id = 3000`, + testkit.Rows(`3000 -1`), + }, + { + `;`, + `select * from t1 where id = 3001`, + testkit.Rows(`3001 -2`), + }, + { + `;`, + `select last_insert_id()`, + testkit.Rows(`3001`), + }, { `insert into t2(id, n) values(1, 1)`, `select * from t2 where id = 1`,