MYSQL numeric alpha code

Asked

Viewed 484 times

0

I want a code INTERNALLY in Mysql a random code, with 2 letters,1 number, 1 letter, 2 numbers

AB-1A-21 - DA-2B-35 - CI-3U-23

how to proceed ?

  • varchar.. by his example, varchar(8)

  • Friend, @rLinhares, I edited the question, what I really want is to know if I can generate this code directly in mysql without php help.

1 answer

1

The default AUTO INCREMENT in Mysql is always int! For your specific case, you will need to create a Function with the criteria you mention above. The column for storing the result is varchar(8).

  • Carlos, thanks for the answer, I edited my question filtering the content better. I want to generate that example in a column of mysql randomly. AS IF IT WERE AUTO-INCREMENT.

  • 1

    You create a Function: https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html CREATE FUNCTION f_increment() RETURNS varchar(8) -- creates hash logic by selecting in the table to see max id and increments 1 ---Return Hence only use as follows Insert into table (id,txt,dt) values (f_increment(),'text',KURDATE())

  • carlos, thank you for your interest in helping. I’ll look at your code.

  • 1

    Another tip is to do with hexadecimal, where you can do addition operations. See link: https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html !

  • my interest is to generate a number of easy decoration. so two 2 letters, 1number+1letter+2numbers. the exadecimal even serves. more does not meet my expectation in reason of the limitation of letters. this will be printed in an adhesive and put at disposal of other people. Soon I will present the system, containing undoubtedly a part of its contribution.

  • my table is complete. I just want to add this column. and I want it to have the values in the model I indicated. are 6 digits instead of 8. I will not change the strokes.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.