I have a table that stores an id from another table, an index for a specific id and some different value
user_id | index_value | some_value |
---|---|---|
1 | 0 | 01 |
1 | 1 | 02 |
1 | 2 | 03 |
2 | 0 | 04 |
3 | 0 | 05 |
3 | 1 | 06 |
1 | 3 | 07 |
I’m about to delete some records and I need to recalculate the data stored in index_value. For example, delete the line with some_value 03.The expected output should look like this:
user_id | index_value | some_value |
---|---|---|
1 | 0 | 01 |
1 | 1 | 02 |
2 | 0 | 04 |
3 | 0 | 05 |
3 | 1 | 06 |
1 | 2 | 07 |
What is the best way to go about this?
2
Answers
Used suggestions from Tim Biegeleisen and Akina. After recalculating the indexes, a query is called to remove unnecessary rows.
I suggest not even maintaining the
index_value
column, but instead generating this computed value at the time you query. Assuming you are using MySQL 8+: