MySQL AutoIncrement character field

こ雲淡風輕ζ 提交于 2019-12-13 04:29:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!