创建由当前年份数字和自动递增的值组成的唯一ID [英] Create a unique ID composed of the current year number and an auto incremented value

查看:69
本文介绍了创建由当前年份数字和自动递增的值组成的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个由4位数字的年份和5位数字的自动递增字段组成的学生证号码.例如:2013-00001,当年份结束时,5位数字将重新开始为零(0),例如:2014-00001.

谢谢,梅塔

解决方案

您可以使用StudentId表本身存储所有数据.

select LatestNumber from StudentIds where Year=2013 ORDER BY LatestNumber DESC LIMIT 1;

将数字与年份分开,因此您可以通过添加虚拟Year = 2014,LatestNumber = 0

在新的一年重置它.

$LatestNumber = $result['LatestNumber'];
$LatestNumber++;

insert into StudentIds (Year, LatestNumber, ...) values (2013, $LastestNumber, ...)

在发生多个查询等情况时添加错误处理...

i want to create a Student ID number compose of 4 digit year and a 5 digit auto increment field. example: 2013-00001 and when the year ends, the 5 digit number will restart to zero (0) example: 2014-00001.

Thanks, Metta

解决方案

You can use the StudentId table itself where you store all the data.

select LatestNumber from StudentIds where Year=2013 ORDER BY LatestNumber DESC LIMIT 1;

Keep the number separate from the year, so you can reset it on a new year by adding a dummy Year=2014, LatestNumber=0

$LatestNumber = $result['LatestNumber'];
$LatestNumber++;

insert into StudentIds (Year, LatestNumber, ...) values (2013, $LastestNumber, ...)

Add error handling in case of multiple queries etc...

这篇关于创建由当前年份数字和自动递增的值组成的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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