问题
I'm creating two tables: Code and Subcode. Each Code can have several subcodes. I would like to label Codes with numbers and subcodes with letters (e.g. 100A, 100B, 100C). How can this be accomplished in MySQL similar to using autoincrement?
回答1:
Auto increment columns must be integer.
One way to implement what you want is to use 16 bits of the column for the code, and the other 16 bits for the sub-code.
MySQL will see a plain integer, and will be able to increment it.
function decode($id) {
$code = ($id >> 16) & 0xFFFF;
$subcode = convert_base($id & 0xFFFF);
}
function encode($code, $subcode) {
return ($code << 16) | convert_base($subcode);
}
(with convert_base
a function to encode the number using A-Z)
来源:https://stackoverflow.com/questions/16762132/mysql-autoincrement-character-field