MySQL AutoIncrement字符字段 [英] MySQL AutoIncrement character field

查看:109
本文介绍了MySQL AutoIncrement字符字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建两个表:代码和子代码.每个代码可以有几个子代码.我想用数字标记代码,用字母标记子代码(例如100A,100B,100C).在MySQL中如何像使用autoincrement一样实现这一目标?

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?

推荐答案

自动增量列必须为整数.

Auto increment columns must be integer.

一种实现所需内容的方法是将列的16位用于代码,将其他16位用于子代码.

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将看到一个普通整数,并将能够对其进行递增.

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);
}

(具有convert_base使用A-Z编码数字的功能)

(with convert_base a function to encode the number using A-Z)

这篇关于MySQL AutoIncrement字符字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆