我想创建一个由4位数年份和5位自动增量字段组成的学生ID编号。例如:2013-00001,当年末结束时,5位数字将重新开始为零(0)例如:2014-00001。
谢谢,Metta
答案 0 :(得分:0)
create Table width two columns: Year,LatestNumber
function generateStudentId(){
$year = date("Y");
$latestNumber = 0;
$res = mysql_query("select * from myTable where Year=$year");
$result = mysql_fetch_array($res);
if($result)
$latestNumber = $result['LatestNumber'];
$latestNumber++;
if($result){
mysql_query("update myTable set LatestNumber=$latestNumber where Year=$year");
}
else
mysql_query("insert into myTable values($year,$latestNumber)");
$fiveDigitNumber = return str_pad((int) $latestNumber,5,"0",STR_PAD_LEFT);
$studentId = $year.$fiveDigitNumber;
return $studentId;
}
答案 1 :(得分:0)
您可以在存储所有数据的地方使用StudentId表。
select LatestNumber from StudentIds where Year=2013 ORDER BY LatestNumber DESC LIMIT 1;
保持号码与年份分开,因此您可以通过添加虚拟年份= 2014,LatestNumber = 0来重置新年度
$LatestNumber = $result['LatestNumber'];
$LatestNumber++;
insert into StudentIds (Year, LatestNumber, ...) values (2013, $LastestNumber, ...)
在多次查询等情况下添加错误处理...