MySQL custom primary key generator

前端 未结 3 1835
梦如初夏
梦如初夏 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 should use a procedure or a trigger. The particular query that builds your newInvoiceId should be something like this:

    SELECT CONCAT(YEAR(now()),count(*)+1) as newInvoiceId
    FROM table
    WHERE InvoiceId like CONCAT(YEAR(now()),'%');
    

    The only part where i'm not 100% confident is the CONCAT(YEAR(now()),'%')

提交回复
热议问题