Incrementing a Primary Key Based on Another Key

I am developing an accounting system for multiple factories in laravel. I have an account that is automatically incremented and is the primary key. I want to have a separate account no. for each factory. those. if the created counter factory A with id 1, its next count should have id 2 instead of some other id like 3,4 like the case id 2 is occupied by the count from factory B. so I want

bill_id  factory_id
   1        A
   1        B
   2        B
   3        B
   2        A 

      

instead

bill_id  factory_id
   1        A
   2        B
   3        B
   4        B
   5        A 

      

I searched a lot but couldn't find a solution. I think this Stack Overflow question is a solution, but can anyone explain it more and also how to implement it in laravel?

+3


source to share


1 answer


TRY:

SELECT * FROM table_name WHERE factory_id='A' ORDER BY bill_id DESC LIMIT 1

      



This will return the last identifier of the particular factory, after which you can execute the INSERT statement incrementally. Please note that bill_id should not be the main one.

0


source







All Articles