I’m trying to find the most efficient way to do something and would appreciate your advice!
I have a table with the following columns:
id
category
category_sequence
other_columns
id is an auto-increment column and the primary key. The category is selected by the user on the UI.
What I’d like to do is generate a category_sequence, which is the highest existing category_sequence for that category + 1.
For example, inserting a few rows would like like:
- 1, 1, 1
- 2, 1, 2
- 3, 2, 1
- 4, 1, 3
and so on.
Obviously I could run a query to extract the highest category_sequence, add one to it, and then run my insert statement. But is there a much more efficient way (some of the inserts will be in fairly big loops, so anything to speed it up, and prevent crossover, would be great).
Thanks in advance,
Kev
you can try
You might have to wrap it in a case statement as well in the case that it is the first record for that category. I don’t have mysql to test it on