简体   繁体   English

MySQL字段AUTO_INCREMENT根据YEAR

[英]MySQL Field AUTO_INCREMENT according to YEAR

I have a table for bills and I need to create the ID field (Primary Key) with this format: 我有一张帐单表格,我需要使用以下格式创建ID字段(主键):

YYYYXXXX -> YEAR+INT(4) Auto_increment

For example: 20150000 , 20150001 , ...., 20159999 例如: 2015000020150001 ,..., 20159999

And it should close at the end of the year and start again at zero in the next year: 20160000 , 20160001 , ... with the posibility to change it manually too. 它应该在明年零收在今年年底开始再次: 2016000020160001 ,......与posibility手动更改它。

Maybe some of you could help me as I'm new to mysql and php. 也许有些人可以帮助我,因为我是mysql和php的新手。

Thanks 谢谢

You need to update auto increment counter to achieve this. 您需要更新自动递增计数器以实现此目的。 Sample code attached. 附带示例代码。

ALTER TABLE  `TABLE_NAME` AUTO_INCREMENT =2015000;

However you need to make sure to update this once one the beginning of 2016 again like : ALTER TABLE TABLE_NAME AUTO_INCREMENT =2016000; 但是,您需要确保在2016年初再次更新一次,例如:ALTER TABLE TABLE_NAME AUTO_INCREMENT = 2016000;

You can make a CRON job & add this conditional logic there : 您可以进行CRON作业并在其中添加此条件逻辑:

alter.php:
<?php
 $conn = mysql_connect(.....);
 $selDb = mysql_select_db(....);
 $sql = 'ALTER TABLE  `TABLE_NAME` AUTO_INCREMENT ='.date("Y").'000';
 mysql_execute($sql);
?>

CRON:
1 1 1 1  * php alter.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM