Mysql 将字母数字VARCHAR条目的值增加1?

Mysql 将字母数字VARCHAR条目的值增加1?,mysql,Mysql,在一个旧项目上,由于没有经过深思熟虑的设计,我有一个列,实际上应该设置为自动增量,但不能设置为自动增量,因为它是字母数字条目,如下所示: c01 c02 c03 c99将继续使用c100和更多,这封信发生在过去,需要对系统进行大修才能将其取出,因此我更喜欢这种解决方法 现在我需要一种方法来模仿SQL语句的自动增量功能,我自己的尝试已经达到了以下程度: INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id, c

在一个旧项目上,由于没有经过深思熟虑的设计,我有一个列,实际上应该设置为自动增量,但不能设置为自动增量,因为它是字母数字条目,如下所示:

c01
c02
c03
c99将继续使用c100和更多,这封信发生在过去,需要对系统进行大修才能将其取出,因此我更喜欢这种解决方法

现在我需要一种方法来模仿SQL语句的自动增量功能,我自己的尝试已经达到了以下程度:

INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id, creation_date, last_edited) VALUES (SELECT(MAX(tag_id)+1), 

'Love', 'All about love', 7, now(), 0);
这一个不能按原样工作,尽管其思想是在列tag_id中选择最高的条目,然后简单地将其增加值1

有没有办法做到这一点


顺便说一句,我也不确定是否可以通过这种方式增加字母数字条目,尽管我知道这是可以做到的,但我不知道怎么做。

如果您想安全地获得c形式的标记id的最大整数值,可以使用以下表达式:

max( convert( substring(tag_id, 2) , unsigned integer) )
^^^ largest   ^^^^^^^^^ after 'c'    ^^^^^^^^^^^^^^^^ convert to positive number
然后您的insert语句将如下所示:

set @newid = convert(
              (select 
               max(convert( (substring(tag_id, 2)) , unsigned integer))+1
               from tags), char(10)
            );

set @newid = if(length(@newid) = 1, concat('0', @newid), @newid);
set @newid = concat('c', @newid);

INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id,
                  creation_date, last_edited)
VALUES (@newid, 'Love', 'All about love', 7, now(), '2012-04-15');

演示:

如果您希望安全地获取c..形式的标记id的最大整数值,可以使用以下表达式:

max( convert( substring(tag_id, 2) , unsigned integer) )
^^^ largest   ^^^^^^^^^ after 'c'    ^^^^^^^^^^^^^^^^ convert to positive number
然后您的insert语句将如下所示:

set @newid = convert(
              (select 
               max(convert( (substring(tag_id, 2)) , unsigned integer))+1
               from tags), char(10)
            );

set @newid = if(length(@newid) = 1, concat('0', @newid), @newid);
set @newid = concat('c', @newid);

INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id,
                  creation_date, last_edited)
VALUES (@newid, 'Love', 'All about love', 7, now(), '2012-04-15');

演示:

这将从c01增加到c02再增加到c03。。。到c99到c100到c101。。。至c999至c1000等

set @nextID = (SELECT CONCAT(SUBSTRING(`tag_id`, 1, 1), IF(CHAR_LENGTH(CAST(SUBSTRING(`tag_id`, 2)
AS UNSIGNED)) < 2, LPAD(CAST(CAST(SUBSTRING(`tag_id`, 2) AS UNSIGNED) + 1 AS CHAR), 2,
'0'), CAST(CAST(SUBSTRING(`tag_id`, 2) AS UNSIGNED) + 1 AS CHAR))) FROM `tags` ORDER BY
`tag_id` DESC LIMIT 1);


INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id,
creation_date, last_edited) VALUES (@nextID, 'Love', 'All about love', 7, NOW(), null);

这将从c01增加到c02再增加到c03。。。到c99到c100到c101。。。至c999至c1000等

set @nextID = (SELECT CONCAT(SUBSTRING(`tag_id`, 1, 1), IF(CHAR_LENGTH(CAST(SUBSTRING(`tag_id`, 2)
AS UNSIGNED)) < 2, LPAD(CAST(CAST(SUBSTRING(`tag_id`, 2) AS UNSIGNED) + 1 AS CHAR), 2,
'0'), CAST(CAST(SUBSTRING(`tag_id`, 2) AS UNSIGNED) + 1 AS CHAR))) FROM `tags` ORDER BY
`tag_id` DESC LIMIT 1);


INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id,
creation_date, last_edited) VALUES (@nextID, 'Love', 'All about love', 7, NOW(), null);

您确定所有身份密钥的格式始终为c..吗?您必须转到c100还是只能转到d01?或者c9A,c9b,c9c,c9d怎么样。。。然后caa cab cac。。cba..cbb..ccc。。。等等,钥匙真的不应该是智能的。它们只需要是唯一的。另一种方法是将其分为两列,并在两列上使用CONCATid_char、id_number,而不仅仅是选择id。根据具体情况,此更改可能不需要太多工作。此外,根据具体情况,这可能会给您留下一个非常有效的系统。您确定所有身份密钥都是c形式的吗?您必须转到c100还是只能转到d01?或者c9A,c9b,c9c,c9d怎么样。。。然后caa cab cac。。cba..cbb..ccc。。。等等,钥匙真的不应该是智能的。它们只需要是唯一的。另一种方法是将其分为两列,并在两列上使用CONCATid_char、id_number,而不仅仅是选择id。根据具体情况,此更改可能不需要太多工作。此外,根据具体情况,这可能会为您提供一个相当高效的系统。