[英]Custom Auto incremented “id” using php and mysql
Hey I am newbie to php and mysql i was trying to generate id for my student table where i want my student id to be like this . 嘿,我是php和mysql的新手,我试图为我的学生表生成ID,我希望我的学生ID像这样。 my student id field is of varchar as well as primary.
我的学生ID字段是varchar还是primary。 $student id= LA/YYYY/0001 and go on where as LA is prefix and YYYY is the year and 0001 is the numeric value so i want the id to be like this and code to be auto incremented.(LA/YYYY/0002) and so on .
$ student id = LA / YYYY / 0001并继续进行操作,因为LA是前缀,YYYY是年份,0001是数字值,所以我希望ID像这样并且代码自动递增。(LA / YYYY / 0002 ) 等等 。 Thank you all for your suggestion .
谢谢大家的建议。
Instead of writing something overly complex to have custom auto incremented ID's, why not rely on MySQL's autoincrementing INT's? 与其编写过于复杂的自定义ID而不是复杂的东西,为什么不依靠MySQL的自动INT? Then, you could have separate columns instead of a composite column for the additional details.
然后,您可以使用单独的列而不是复合列来获取其他详细信息。 In your code, you could then concatenate the columns into one output.
在您的代码中,然后可以将列连接为一个输出。
Example: 例:
id | prefix | year | first_name | last_name
-------------------------------------------
1 LA 2014 Half Crazed
2 FR 2015 Jon Doe
Then, in your application (or SQL): 然后,在您的应用程序(或SQL)中:
SELECT CONCAT(prefix, '/', year, '/', LPAD(id, 4, '0')) AS uuid
(note: untested, for demonstration only) (注意:未经测试,仅用于演示)
If you absolutely need to use the auto-incremented (for every record) you can use a composite key as an ID. 如果您绝对需要使用自动递增(针对每个记录),则可以使用复合键作为ID。
So your table will have two fields contributing to the key: 1. id (auto-incremented integer) 2. prefix (varchar with format LA/YYYY) 因此,您的表将有两个字段构成键:1. id(自动递增的整数)2.前缀(格式为LA / YYYY的varchar)
You can concatenate both to produce the id you are looking for. 您可以将两者串联以生成您要查找的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.