How to get next alpha-numeric ID based on existing value from MySQL

前端 未结 6 515
我在风中等你
我在风中等你 2020-12-19 07:28

First, I apologize if this has been asked before - indeed I\'m sure it has, but I can\'t find it/can\'t work out what to search for to find it.

I need to generate un

6条回答
  •  爱一瞬间的悲伤
    2020-12-19 08:03

    If you are using MyISAM, then you can create a compound primary key on a text field + auto increment field. MySQL will handle incrementing the number automatically. They are separate fields, but you can get the same effect.

    CREATE TABLE example (
    company_name varchar(100),
    key_prefix char(4) not null,
    key_increment int unsigned auto_increment,
    primary key co_key (key_prefix,key_increment)
    ) ENGINE=MYISAM;
    

    When you do an insert into the table, the key_increment field will increment based on the highest value based on key_prefix. So insert with key_prefix "smit" will start with 1 in key_inrement, key_prefix "jone" will start with 1 in key_inrement, etc.

    Pros:

    • You don't have to do anything with calculating numbers.

    Cons:

    • You do have a key split across 2 columns.
    • It doesn't work with InnoDB.

提交回复
热议问题