在同一个表中关于 INSERT...SELECT MAX(x)+1 中引起的错(mysql)

是这样的,在一个表中有个sort(int)排序字段,每次插入数据时要求sort能自+1,效果像主键id自动加1那样。但mysql中一个有只允许主键自动+1,所以只能用sql实现。

起初是这样写:

insert into tt(sort,name) values(select (max(sort)+1)as sort from tt,'test_name');

这是想当然的做法,肯定是不能这样写的。

后来在网上搜索到答案了

insert into tt(sort,name) select (max(sort)+1),'test_name' from tt

这样,就ok了,

来自:http://bugs.mysql.com/bug.php?id=3575

原文地址:https://www.cnblogs.com/dingchenghong/p/2561132.html