1

I have the string like this: POyyyymm0000

At the current time: when i am inputting, it will be PO2017070001

And i input again it will be PO2017070002

And in the next month it will be PO2017080001

The question is how to insert PO[thecurrentyear][thecurrentmonth][autoincrement, start from 0000 to 9999]

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38
  • 1
    Possible duplicate of [How to make MySQL table primary key auto increment with some prefix](https://stackoverflow.com/questions/17893988/how-to-make-mysql-table-primary-key-auto-increment-with-some-prefix) – Doug Jul 06 '17 at 07:32
  • Why do you want this? – Doug Jul 06 '17 at 07:35
  • use triggers as shown here: https://stackoverflow.com/questions/17893988/how-to-make-mysql-table-primary-key-auto-increment-with-some-prefix – Dimgold Jul 06 '17 at 07:39

1 Answers1

0

You can use date() and rand() to achieve this
Just like this

$id='PO'.date('Ymd').rand(0000,9999);

It will produce output as

enter image description here

Modified:-
For unique key you can use time() like this

$id='PO'.date('Ymd').time(); //o/p is "PO201707061499326403"

It will never repeat the same id.

Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51
  • It's not technically auto-incrementing the final 4 digits this way. – Doug Jul 06 '17 at 07:30
  • you are not storing the previous no. any where then how can you get the next no. in auto increment .Actually what you want ? to make it unique or any thing else ? – Bibhudatta Sahoo Jul 06 '17 at 07:32
  • It's not my question :) I'd be happy with a normal digit ID personally. There is another option here though https://stackoverflow.com/questions/17893988/how-to-make-mysql-table-primary-key-auto-increment-with-some-prefix – Doug Jul 06 '17 at 07:34
  • But i want (in the next month) or (in the next year) the autoincreement reset to 0000 – Hoàng Nguyễn Jul 06 '17 at 07:34
  • @HoàngNguyễn just try my modified answer it will help you. – Bibhudatta Sahoo Jul 06 '17 at 07:36