MySQL custom primary key generator

前端 未结 3 1839
梦如初夏
梦如初夏 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:42

    A warning against naive year/week like 2012/01. YEAR(NOW()) being wrong.

    Better use YEARWEEK(NOW()). Gives a number like 201201

    The following shows that year is not necessarily year-of-week-number:

    mysql> SELECT YEARWEEK('1987-01-01');
           -> 198652
    

    For the rest having year and week in one single int, is in fact simpler.

    int year = yearweek / 100;
    int week = yearweek % 100;
    

提交回复
热议问题