0

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

谢谢, 慈

4

2 回答 2

0

您可以使用 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, ...)

在多个查询等情况下添加错误处理......

于 2013-03-03T20:36:05.350 回答
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;
}
于 2013-03-03T11:04:18.537 回答