2
I have a table (groups
) in SQL with the following structure:
id | name | description | display_order | ...
The field display_order
was defined as UNIQUE
and must be an integer. Its main function is to be used to create a custom order in the table, through the ORDER BY
:
SELECT * FROM groups ORDER BY display_order;
However, I need to create a query to move this order up or down, so that I also change the top and bottom records.
See a representative image what I need to do: http://prntscr.com/jjrxh1.
+----+-----------------+-------------+--------------------+
| id | name | description | display_order (↓) |
+----+-----------------+-------------+--------------------+
| 1 | Administradores | ... | 1 |
+----+-----------------+-------------+--------------------+
| 3 | Outro Grupo | ... | 2 |
+----+-----------------+-------------+--------------------+
| 2 | Moderadores | ... | 3 |
+----+-----------------+-------------+--------------------+
| 4 | Grupo X | ... | 4 |
+----+-----------------+-------------+--------------------+
Assuming, therefore, that I need to move the "Moderators" group up as shown in the image, based on ID 2, what the query would look like?
In case, I would need a query to exchange the
display_order
3
with thedisplay_order
2
. The problem is: I don’t know how to do this query.– Luiz Felipe