简体   繁体   English

MySQL ID按适当的系列

[英]MySQL ids by proper series

We are building an invoice management app and need help/suggestions with the following... 我们正在开发一个发票管理应用程序,需要以下帮助/建议...

  1. Invoice should start with 1 and according to series like 1 2 3 4 5 6 7 8 not 1 3 4 7 发票应以1开头,并按1 2 3 4 5 6 7 8这样的序列而不是1 3 4 7
  2. It should start with 1 again each financial year for example it should start with 1 again in 2012 它应该在每个财政年度重新从1开始,例如,在2012年应该重新从1开始
  3. how to show all invoices older than febuary 2011 and newer than march 2010 means financial year 如何显示所有早于2011年2月和新于2010年3月的发票表示财务年度

We want your suggestions to achieve this. 我们希望您的建议能够实现这一目标。 thanks. 谢谢。

you need compound primary key. 您需要复合主键。 eg: 例如:

year int
invoice_id int
  1. The incrementing ID is trivial in MySQL. 在MySQL中,递增ID很小。 A simple auto_Increment primary key column does the trick. 一个简单的auto_Increment主键列即可解决问题。
  2. Having the number wrap back to 1 each year can't be done with vanilla SQL without triggers and/or client-side involvement. 如果没有触发器和/或客户端的参与,使用香草SQL不可能将每年的数字换回1。
  3. The fiscal year business is more of a client-side issue. 会计年度的业务更多是客户端问题。 Simply store a "created on" timestamp with each record and then decide on the filtering rules in the client, then you simply do select ... where date_of_record between $start_of_period and $end_of_period . 只需为每个记录存储一个“创建时间”时间戳,然后确定客户端中的过滤规则,然后只需select ... where date_of_record between $start_of_period and $end_of_period

Create the primary key as in heximal's answer. 按照十六进制答案创建主键。 Then use this to insert a new id: 然后使用它插入新的ID:

insert into my_table (id, year)
   values (
      coalesce (
      (
         select new_id = max(id) + 1
         from my_table
         where year = @year
      ), 1)
      , @year
   )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM