MySQL - varchar 中的 max(column)+1 仅适用于整数
MySQL - max(column)+1 in a varchar only for integers
我有一列整数,例如 0000000001、0000000002,有时还有 xxxxxxxxxx,varchar 类型长度为 10。
我只需要获得最大(列)+1 或 0000000002,以便遵循 0000000003。
但现在因为里面的 xxxxxxxxxx 又从 0000000001 开始了。
SELECT 最大值(ID)+1
将数字添加到不是数字的字符串时,它会 return 数字。
'foo' + 42 = 42
因为你 select MAX(ID)
它 return 是 'xxxxxxxxxx'。
最简单的解决方法是稍微调整一下计算。
SELECT max(ID+1) from yourtable
因为这样,从 'xxxxxxxxxx'+1 得到 return 的 1 将不是 MAX。
或select大于0的
select LPAD(MAX(ID)+1, 10, '0')
from yourtable
where id > 0
我有一列整数,例如 0000000001、0000000002,有时还有 xxxxxxxxxx,varchar 类型长度为 10。 我只需要获得最大(列)+1 或 0000000002,以便遵循 0000000003。 但现在因为里面的 xxxxxxxxxx 又从 0000000001 开始了。
SELECT 最大值(ID)+1
将数字添加到不是数字的字符串时,它会 return 数字。
'foo' + 42 = 42
因为你 select MAX(ID)
它 return 是 'xxxxxxxxxx'。
最简单的解决方法是稍微调整一下计算。
SELECT max(ID+1) from yourtable
因为这样,从 'xxxxxxxxxx'+1 得到 return 的 1 将不是 MAX。
或select大于0的
select LPAD(MAX(ID)+1, 10, '0')
from yourtable
where id > 0