首页 > 使用一个表来填充另一个表,期待插入数据的ID从1开始,但ID也被选择过来了

使用一个表来填充另一个表,期待插入数据的ID从1开始,但ID也被选择过来了

我创建了如下两个表:

CREATE TBALE A(id integer primary key, max float);
CREATE TBALE B(id integer primary key, max float);

在A表中插入数据后,我用如下语句在B表中插入数据:

INSERT INTO B(max) SELECT MAX(max) from A;

这里便出现了我提到的问题,我期待B表中的第一条数据id是从1开始的,但是执行执行这条语句后,id并不是1,而是把A表中对应的id给选择过来了。即如果A表中最大值的id是33的话,那么B表中新插入数据的id也是33。
期待高手指点迷津。


哪种数据库? 版本?

mysql 5.5.24没看到你说的现象. B表的id没有设default, 也没有AUTO_INCREMENT, 那么默认应该是0.

update

马上试了一下sqlite3, 没看到你说的问题.

SQLite version 3.8.4.3 2014-04-03 16:53:12
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> CREATE TabLE B(id integer primary key, max float);
sqlite> CREATE TabLE A(id integer primary key, max float);
sqlite> insert into A values(34, 2.1);
sqlite> insert into A values(343, 32.1);
sqlite> INSERT INTO B(max) SELECT MAX(max) from A;
sqlite> select * from b;
1|32.1
sqlite> select * from a;
34|2.1
343|32.1
sqlite>
【热门文章】
【热门文章】