How to Auto Increment ID Numbers with Letters and Numbers

后端 未结 5 1457
眼角桃花
眼角桃花 2021-01-03 13:54

How to Auto Increment ID Numbers with Letters and Numbers, example \"KP-0001\" it will increment to \"KP-0002\"

Thank you!

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 14:13

    here is a useful article

    • auto increment with a string of numbers and letters

    But basically I encourage you to create your own algorithm on this. You can add that algorithm in BEFORE INSERT trigger. Or you can do that on the front-end.

    Example of pseudocode for the algorthm

    • get the lastID [KP-0001]
    • remove some characters and put it in a variable [KP-]
    • convert the remaining into number since it's a string [0001]
    • increment by 1 [1 + 1 = 2]
    • convert it back to string and pad zero on the right [0002]
    • concatenate the variable and the newly incremented number [KP-0002]
    • save it.

提交回复
热议问题