如何创建一个自动递增的字符串?

时间:2019-02-07 10:07:05

标签: mysql sql string

我遇到了用01、02,...,10、12递增字符串的问题

  • 我有一个SQL查询,给我AAAA06,这在我的数据库中最大。
  • 现在我有一个要求,每当用户调用此命令时,它将使用最大编号检查数据库。当前为AAAA06(当前为最大值),应该返回或打印AAAA07
  • 类似地,当最大值为AAAA09时,它应该增加到AAAA10,依此类推
  • 作为一种语言,我正在使用Java,但是我想知道我可以通过自己的sql查询自身实现此目的还是必须编写Java代码来实现这一目标?

我正在使用的查询就像

select max(code) from mt_users where maincode='AAAA'

打电话给我最大电话号码是我做的;我只想增加或连接适合获得期望输出的任何内容。

它返回我AAAA06,因为这是当前的最大值。

重要点

  1. 最初为AAAA时应显示AAAA01
  2. 当它为AAAA09时应显示AAAA10
  3. 当它为AAAA99时将显示AAAA100

注意::目前,我的最大值为AAAA06,但是对于将来的新用户来说,它可以是AAAE,因此我必须以AAAE01开始。

2 个答案:

答案 0 :(得分:1)

您可以将@Override public void onItemSelected(AdapterView<?> parent, View view, int i, long l) { if(mMap == null) return; Polyline polyline1 = mMap.addPolyline(new PolylineOptions() .clickable(true) .add( new LatLng(-35.016, 143.321), new LatLng(-34.747, 145.592), new LatLng(-34.364, 147.891), new LatLng(-33.501, 150.217), new LatLng(-32.306, 149.248), new LatLng(-32.491, 147.309))); } 用作

lpad

根据我们之间的聊天情况,您需要以下逻辑:

select case when code < 10 
            then concat( substring(maincode,1,4),lpad( max(code)+1 ,length(code+10),'0')) 
            else concat( substring(maincode,1,4),lpad( max(code)+1 ,length(code+1),'0')) 
            end
    as "Result String"
  from tab
 where maincode = 'AAAA01' -- 'AAAE';

Demo

答案 1 :(得分:1)

简短答案-使用此查询:

SELECT id AS PrevID, CONCAT(
    SUBSTRING(id, 1, 4),
    IF(CAST(SUBSTRING(id, 5) AS UNSIGNED) <= 9, '0', ''),
    CAST(SUBSTRING(id, 5) AS UNSIGNED) + 1
) AS NextID
FROM (
    -- since you allow strings such as AAAA20 and AAAA100 you can no longer use MAX
    SELECT id
    FROM t
    ORDER BY SUBSTRING(id, 1, 4) DESC, CAST(SUBSTRING(id, 5) AS UNSIGNED) DESC
    LIMIT 1
) x

结果:

| PrevID  | NextID  | 
| AAAA100 | AAAA101 | 
| AAAA21  | AAAA22  | 
| AAAA06  | AAAA07  | 

只是为了好玩,我编写了这个存储过程,它生成了类似于AAAA00 AAAA99 AAAB00等的数字

CREATE FUNCTION NextID(PrevID VARCHAR(6))
RETURNS VARCHAR(6)
BEGIN
    DECLARE s VARCHAR(4);
    DECLARE i INT;
    DECLARE j INT;

    SET s = LEFT(PrevID, 4);
    SET s = REPLACE(s, 'A', '0');
    SET s = REPLACE(s, 'B', '1');
    SET s = REPLACE(s, 'C', '2');
    SET s = REPLACE(s, 'D', '3');
    SET s = REPLACE(s, 'E', '4');
    SET s = REPLACE(s, 'F', '5');
    SET s = REPLACE(s, 'G', '6');
    SET s = REPLACE(s, 'H', '7');
    SET s = REPLACE(s, 'I', '8');
    SET s = REPLACE(s, 'J', '9');
    SET s = REPLACE(s, 'K', 'A');
    SET s = REPLACE(s, 'L', 'B');
    SET s = REPLACE(s, 'M', 'C');
    SET s = REPLACE(s, 'N', 'D');
    SET s = REPLACE(s, 'O', 'E');
    SET s = REPLACE(s, 'P', 'F');
    SET s = REPLACE(s, 'Q', 'G');
    SET s = REPLACE(s, 'R', 'H');
    SET s = REPLACE(s, 'S', 'I');
    SET s = REPLACE(s, 'T', 'J');
    SET s = REPLACE(s, 'U', 'K');
    SET s = REPLACE(s, 'V', 'L');
    SET s = REPLACE(s, 'W', 'M');
    SET s = REPLACE(s, 'X', 'N');
    SET s = REPLACE(s, 'Y', 'O');
    SET s = REPLACE(s, 'Z', 'P');

    SET i = RIGHT(PrevID, 2);
    SET j = CONV(s, 26, 10);

    SET i = i + 1;
    IF i > 99 THEN
        SET i = 0;
        SET j = j + 1;
    END IF;

    SET s = CONV(j, 10, 26);
    SET s = REPLACE(s, 'P', 'Z');
    SET s = REPLACE(s, 'O', 'Y');
    SET s = REPLACE(s, 'N', 'X');
    SET s = REPLACE(s, 'M', 'W');
    SET s = REPLACE(s, 'L', 'V');
    SET s = REPLACE(s, 'K', 'U');
    SET s = REPLACE(s, 'J', 'T');
    SET s = REPLACE(s, 'I', 'S');
    SET s = REPLACE(s, 'H', 'R');
    SET s = REPLACE(s, 'G', 'Q');
    SET s = REPLACE(s, 'F', 'P');
    SET s = REPLACE(s, 'E', 'O');
    SET s = REPLACE(s, 'D', 'N');
    SET s = REPLACE(s, 'C', 'M');
    SET s = REPLACE(s, 'B', 'L');
    SET s = REPLACE(s, 'A', 'K');
    SET s = REPLACE(s, '9', 'J');
    SET s = REPLACE(s, '8', 'I');
    SET s = REPLACE(s, '7', 'H');
    SET s = REPLACE(s, '6', 'G');
    SET s = REPLACE(s, '5', 'F');
    SET s = REPLACE(s, '4', 'E');
    SET s = REPLACE(s, '3', 'D');
    SET s = REPLACE(s, '2', 'C');
    SET s = REPLACE(s, '1', 'B');
    SET s = REPLACE(s, '0', 'A');

    RETURN CONCAT(LPAD(s, 4, 'A'), LPAD(i, 2, '0'));
END

SELECT NextID('AAAA01') -- AAAA02
SELECT NextID('AAAA99') -- AAAB00
SELECT NextID('AAAB99') -- AAAC00
SELECT NextID('AAAZ99') -- AABA00