MySQL custom primary key generator

前端 未结 3 1834
梦如初夏
梦如初夏 2021-01-27 04:14

I have written an invoice module for our reservation system.

So when I create a new invoice, I automatically generate a primary key through MySQL.

However for th

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-27 04:35

    You can make two fields in your, year and id.

    Make one primary key on both fields, giving id the auto_increment option. For each unique value of year, id will be counting up. For example:

    2012    1
    2012    2
    2012    3
    2013    1
    

    And you can concat them when selecting: SELECT CONCAT(year,id) AS primary FROM table

    Inserting will be:

    INSERT INTO table SET year = YEAR(NOW())

    you don't have to specify id.

提交回复
热议问题