Generating a 20-bit primary key with a UUID
SELECT Left (REPLACE (UUID (), '-', '), and () from DUAL
Another way:
Because there are letters in the database
Remove letters when sorting, re-fetch maximum, and add 1 to new primary key
SELECT ifnull (dept_id,0) +1 from Base_dept ORDER by dept_id+0 DESC LIMIT 0,1
Or
Use the MySQL function Cast/convert:
MySQL provides us with two types of conversion functions: cast and convert, how can we let go of the ready-made stuff?
The CAST () and CONVERT () functions can be used to get a value of one type and produce a value of another type.
This type can be one of the following values:
binary[(N)]
char[(N)]
DATE
Datetime
DECIMAL
Signed [INTEGER]
Time
UNSIGNED [INTEGER]
So we can also use cast to solve the problem:
as signed) DESC LIMIT 0, 1;
You can also use convert to fix this problem:
SELECT ifnull (dept_id,0) +1 from Base_dept ORDER by CONVERT (dept_id,signed) DESC LIMIT 0, 1;
MySQL generate varchar type primary key sort