问题描述
是否可以在MySQL中制作结合了alpha元素和数字的自动增量功能?
Is it possible to make an auto-increment function in MySQL that combines alpha elements and numeric?
我有一个具有QAb99,QAb101,QAd9003等键的现有系统.最大数字部分的范围为1到9999,而字母以QA开头,范围从QAa到QAd等,最终将通过QAd9999到QAe1等
I have an existing system with keys like QAb99, QAb101, QAd9003, etc. The max numeric portion ranges from 1 to 9999, while the letters begin with QA and range from QAa to QAd, etc. and will eventually pass QAd9999 to QAe1, etc.
在SQL或SQL外部(例如php脚本)管理生成新密钥是否更好?
Is it better to manage generating new keys in SQL or outside of SQL (i.e. php script)?
谢谢.
推荐答案
我前一段时间问过这个问题.不幸的是,Mysql并没有这样做.我很喜欢它,但事实并非如此.在PHP中,你可以做到这一点.示例:
I asked this a while ago. Mysql doesn't do this unfortunately. I'd love it to, but it just doesn't. In php you could do it. Example:
public function random_id_gen($length)
{
//the characters you want in your id
$characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$max = strlen($characters) - 1;
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, $max)];
}
return $string;
}
这篇关于复合(字母数字)主键和自动递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!