I have a table which contains a bunch of rows and a sort_order. I was wondering how I can do this so when I delete a row, say I delete the highlighted row with a sort_order of 7. Then instead of 7 just being removed, I would need to change #8 to #7 .. #9 to #8 .. #10 to #9 .. and #11 to #10.
Is something like this possible?
An "ALTER TABLE ORDER BY" statement exist in the syntaxes accepted by MySQL. According to the documentation, this syntax: - only accept *one* column, as in "ALTER TABLE t ORDER BY col;" - is used to reorder physically the rows in a table, for optimizations.
You can't DELETE TOP (x) with an ORDER BY.
To delete rows in a MySQL table, use the DELETE FROM statement: DELETE FROM products WHERE product_id=1; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.
Right-click in a table cell, row, or column you want to delete. On the menu, click Delete Cells. To delete one cell, choose Shift cells left or Shift cells up. To delete the row, click Delete entire row.
If you really need to change these values (sort_order
will still work with gaps), you can
Delete From t
Where sort_order = 7
and then reduce the remaining entries by 1
:
Update t
Set sort_order = sort_order - 1
Where sort_order > 7
Like this:
Update tablename
Set sort_order = sort_order - 1
where sort_order > 7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With