1

I'm having some problem with the website I'm creating. I need to create a tracking number like this ABC-0001-15.

  • The ABC will depend on the department
  • The 0001 is an incremental from the database
  • and 15 is the year

I want it to go back to 0001 if the year changes.

How can I achieve this ? I've done my research but I can't find how to, or I don't know the keyword to use.

I found this article on this site How to make MySQL table primary key auto increment with some prefix

but I need the user of the website to see the auto-number

Community
  • 1
  • 1
Audrey
  • 161
  • 1
  • 2
  • 9
  • I am not totally sure if I understand your question correctly, but lets see, in my opinion, saving a key like that would be best to be done with multiple columns(i.e. ABC goes in department, 0001 goes in dep_version and 15 goes in year). Then you have a transaction to your system like: select max(dep_version) from table where department = ABC; increment dep_version by 1, and store it with the rest of your data. – Techno Dec 27 '15 at 00:56
  • Plus you could make all 3 columns primary key for consistency. – Techno Dec 27 '15 at 01:01
  • Welcome to Stack Overflow. I have fixed your English issues and clarified the question. Please post what you have tried so far. – Rohit Gupta Dec 27 '15 at 01:44

1 Answers1

0

Why don't you just create an empty record, then get its id and update it afterwards... if need be?

MistaJase
  • 839
  • 7
  • 12