简体   繁体   English

使用php和mysql自定义自动递增的“ id”

[英]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.

相关问题 使用预准备语句在MySQL中插入自动增量ID的问题 - Issue On Inserting Auto Incremented ID in MySQL Using Prepared Statement 在MySQL上使用PDO时存储自动递增ID - Storing Auto-Incremented ID when using PDO for MySQL 如何在php中插入mysql的自动递增字段ID,并将一个表的ID更新为另一个表? - How to insert the auto incremented field id of mysql in php and update the id of one table to another? MYSQL + PHP删除表中的行时如何更新自动递增的ID? - MYSQL+PHP How to update auto-incremented id when delete row in table? 使用php插入值后,如何获取行的自动递增ID? - How to get the auto-incremented ID for a row once you insert values using php? PHP中的多行语句到MySQL数据库(重新排列自动递增的ID) - Multi line statement in PHP to MySQL database (resorting auto incremented IDs) 删除MySQL中以php为目标的auto_incremented int记录? - Delete record in MySQL with php targeting auto_incremented int? mysql和PHP中的自动增量ID - Auto Increment ID in mysql and PHP 使用codeigniter,并且需要使用oci8获取下一个查询的上一个自动递增的ID的ID - using codeigniter and need to get id of last auto-incremented id for next query, using oci8 使用angularjs php和mysql的字母数字自动增量ID - alphanumeric auto increment id using angularjs php and mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM