OK I see your point but I have been searching for a way to actually renumber the fields.
So, I took the time to look at this more closely and came up with this, perhaps you may find this a little inefficient but I thought I'd post it anyway for some comments.
Code:
-- Increase the value of the ranking
REPLACE products SELECT
id,
name,
CASE WHEN ranking = 2 THEN 4
WHEN ranking > 4 THEN ranking
WHEN ranking > 2 THEN ranking - 1
ELSE ranking
END AS ranking
FROM
products
Code:
-- Decrease the value of the ranking
REPLACE products SELECT
id,
name,
CASE WHEN ranking = 4 THEN 2
WHEN ranking > 4 THEN ranking
WHEN ranking >= 2 THEN ranking + 1
ELSE ranking
END AS ranking
FROM
products
So either of those queries will increase or decrease the value of the ranking fields and all the other ranking fields relative to the one you're changing. Of course in order to fill in these queries correctly you need to know the value of the ranking field you are changing it from and the value you're changing it to.
Now the thing is I notice that when performing this query across five rows it actually performs 10 instructions. So the record first gets deleted then inserted again. Obviously this is not going to be very efficient if you have 1,000 records across which to perform the query. I guess it's going to very rare to be doing this where you don't have a WHERE clause to restrict the rows.
Anyway, I'd be interested to hear your thoughts.
Thx
Bookmarks