Skip to content

Instantly share code, notes, and snippets.

@amirkhiz
Created January 6, 2019 14:43
Show Gist options
  • Save amirkhiz/3c6042fafca86bb7c33c9fc11ce34e9a to your computer and use it in GitHub Desktop.
Save amirkhiz/3c6042fafca86bb7c33c9fc11ce34e9a to your computer and use it in GitHub Desktop.
Sort Queries
#Forwarding
#ex. : from = 1, to = 5
UPDATE table_name SET order_field = order_field - 1
WHERE order_field > {from}
AND order_field <= {to}
#Backward
#ex. from = 5, to = 1
UPDATE table_name SET order_field = order_field + 1
WHERE order_field >= {to}
AND order_field < {from}
#Run this after both update query:
UPDATE table_name SET order_field = {to}
WHERE id_field = {from_row_id}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment