How can I reorder rows in sql database
Asked Answered
sql
S

16

55

Is it possible to reorder rows in SQL database? For example; how can I swap the order of 2nd row and 3rd row's values?

The order of the row is important to me since i need to display the value according to the order.


Thanks for all the answers. But 'Order by' won't work for me.

For example, I put a list of bookmarks in database. I want to display based on the result I get from query. (not in alphabet order). Just when they are inserted.

But user may re-arrange the position of the bookmark (in any way he/she wants). So I can't use 'order by'.

An example is how the bookmark display in the bookmark in firefox. User can switch position easily. How can I mention that in DB?

Thank you.

Sideling answered 1/5, 2009 at 18:34 Comment(0)
O
95

It sounds like you need another column like "ListOrder". So your table might look like:

BookMark ListOrder
======== =========
  d        1
  g        2
  b        3
  f        4
  a        5

Then you can "order by" ListOrder.

Select * from MyTable Order By ListOrder

If the user can only move a bookmark one place at a time, you can use integers as the ListOrder, and swap them. For example, if the user wants to move "f" up one row:

Update MyTable
    Set ListOrder=ListOrder+1
        Where ListOrder=(Select ListOrder-1 From MyTable where BookMark='f')

Update MyTable
    Set ListOrder=ListOrder-1
        Where BookMark='f'

If the user can move a bookmark up or down many rows at once, then you need to reorder a segment. For example, if the user wants to move "f" to the top of the list, you need to:

if (increment) {
  update MyTable
    Set ListOrder=ListOrder-1
        where ListOrder<=1 -- The New position
            and ListOrder >(Select ListOrder from MyTable where BookMark='f')
} else {
  update MyTable
    Set ListOrder=ListOrder+1
        where ListOrder>=1 -- The New position
            and ListOrder <(Select ListOrder from MyTable where BookMark='f')
}

 update MyTable
     Set ListOrder=1 -- The New Position
         Where Bookmark='f'
Ornate answered 1/5, 2009 at 19:30 Comment(9)
Thank you for this idea. I guess I was trying to see if I can solve my problem without creating a new Coloumn.Sideling
But can this work if a bookmark is move more than 1 place at a time? For example, what if the user want to move "f" from 4 to 2? all g, b, a need to be updated.Sideling
Thanks you Mike. I am not sure how to read your SQL example. What is the SQL statement and what are the values I need to fill in? update MyTable Set ListOrder=ListOrder+1 where ListOrder>=1 -- The New position and ListOrder <(Select ListOrder from MyTable where BookMark='f') update MyTable Set ListOrder=1 -- The New Position Where Bookmark='f'Sideling
Sorry, but this website didn't paste the code well. Two dashes -- indicate that the rest of the line is a comment Unfortunately pasting rearranged my lines. update MyTable Set ListOrder=ListOrder+1 where ListOrder>=1 and ListOrder <(Select ListOrder from MyTable where BookMark='f') update MyTable Set ListOrder=1 Where Bookmark='f'Ornate
I tried it but it doesn't quite work: update test Set ordering=ordering+1 where ordering>=@newPosition and ordering <(Select ordering from test where id=@rowId) update test Set ordering=@newPosition Where id=@rowId I ended up with two rows having the same order.Greggs
what about move d to the last position?Peppel
Remember to put the two UPDATE queries in 1 transaction (at the appropriate isolation level).Fulgurite
It would be nice if this could work with a unique order constraint. The intermediary updates seem to render a constraint impossible.Ipoh
Looking at the second example, I can see how you can move an item from a higher position to a lower position (position 10 to position 3), but it does not look like it will work by moving from a lower position to a higher position (position 3 to position 10).Inexorable
W
24

As others have mentioned, it's not a good idea to depend on the physical order of the database table. Relational tables are conceptually more like unordered sets than ordered lists. Assuming a certain physical order may lead to unpredictable results.

Sounds like what you need is a separate column that stores the user's preferred sort order. But you'll still need to do something in your query to display the results in that order.

It is possible to specify the physical order of records in a database by creating a clustered index, but that is not something you'd want to do on an arbitrary user-specified basis. And it may still lead to unexpected results.

Wafer answered 1/5, 2009 at 18:52 Comment(0)
U
8

Use ORDER BY in your SELECT query. For example, to order by a user's last name, use:

SELECT * FROM User ORDER BY LastName
Upland answered 1/5, 2009 at 18:37 Comment(0)
J
7

A little late to the party, but anyone still looking for an answer to this problem, you need to use the Stern-Brocot technique.

Here's an article explaining the theory behind it

For each item you need to store a numerator and denominator. Then you can also add a computed column which is the division of both. Each time you move an item inbetween 2 others, the item's numerator becomes the sum of both neighboring numerators, and the item's denominator becomes the sum of both neighboring denominators.

These numbers won't skyrocket as fast as with the "averaging" method, where you lose all accuracy after 17 swaps.

I also created a demo where the method is implemented.

Jehias answered 4/11, 2021 at 16:57 Comment(2)
Assuming this technique works, this looks like the most efficient solution of all presented because it always only requires to update one record.Weimaraner
Up to a certain point. If the user manages to choose the right order of reordering, he can manage to screw things up after 50 swaps. In the demo, you need to let the numbers grow as fast as possible. Thus, the reliability of this method depends on the number of items in the list...Jehias
A
5

The order of the rows on the actual database should not matter.

You should use the ORDER BY clause in your queries to order them as you need.

Aggress answered 1/5, 2009 at 18:38 Comment(0)
B
5

Databases can store the data in any way they want. Using the "order by" clause is the only way to guarantee an ordering of the data. In your bookmark example, you could have an integer field that indicates the ordering, and then update that field as a user moves things around. Then ORDER BY that column to get things in the right order.

Blatherskite answered 1/5, 2009 at 18:52 Comment(0)
I
3

I have a solution for this that I have used a few times. I keep an extra field "sort_order" in the table, and update this when reordering. I've used this in cases when I have some sort of containers with items, and the order of the items should be editable inside the container. When reordering, I only update the sort_order for the items in the current container, which means not to many (usually in practice only a few) rows have to be updated.

In short, I do the following:

  1. add a sort_order field to the items table
  2. when inserting a new row, I set sort_order=id
  3. when reordering (needs id of item to move, and id of item to insert after):
    • select id, sort_order from items where container = ID order by sort_order
    • split the id and sort_order from rows in two arrays
    • remove the id of the item to move from the id-list
    • insert the id of the item to move after the id of the item to insert after
    • merge the list of ids and the list of sort_order into a two dimensional array, as [[id, sort_order], [id2, sort_order], ...]
    • run update item set sort_order=SORT_ORDER where id=ID (executemany) with merged list

(If moving item to another container, after updating "container foreign key" move first or last depending on app.)

(If the update involves a large number of items, I do not think this solution is a good approach.)

I have made an example using python and mysql on http://wannapy.blogspot.com/2010/11/reorder-rows-in-sql-database.html (copy and try it) along with some extra explanations.

Ionogen answered 8/11, 2010 at 14:38 Comment(0)
A
2

As others have stated use an order by.
Never depend on the order data exists in a physical table, always base it of the data you are working with, be it one or more key fields.

Agency answered 1/5, 2009 at 18:42 Comment(0)
H
2

First, let me agree with everyone here that the order in the table shouldn't matter. Use a separate [SortOrder] column that you update and include an Order By clause.

That said, SQL Server databases do allow for a single "clustered index" on a table that will actually force the position in the underlying table storage. Primarily useful if you have a big dataset and always query by something specific.

Harping answered 1/5, 2009 at 21:54 Comment(0)
L
2

Add a position column to your table and store as a simple integer.

If you need to support multiple users or lists, your best bet is to create a bookmarks table, an users table and a table to link them.

  • bookmarks: id,url
  • users: id,name
  • users_bookmarks: user_id, bookmark_id, position, date_created

Assuming date_created is populated when inserting rows you can get secondary list ordering based on date.

select bookmark_id from users_bookmarks where user_id = 1 order by position, date_created;
Lustrate answered 18/5, 2009 at 14:57 Comment(0)
C
2

At times like this, I am reminded of a quote from the Matrix: "Do not try and order the database. That's impossible. Instead, only realize the truth... there is no order. Then you will see that it the table that orders itself, it is you who orders the table."

When working with MySQL through a GUI, there is always a decision to make. If you run something like SELECT * FROM users, MySql will always make a decision to order this by some field. Normally, this will be the primary key.

+----------------
| id | name     |
-----------------
| 1  | Brian    |
| 2  | Carl     |
| 3  | Albert   |
-----------------

When you add an ORDER BY command to the query, it will make the decision to order by some other field.

For Example Select * From users ORDER BY name would yield:

+----------------
| id | name     |
-----------------
| 3  | Albert   |
| 1  | Brian    |
| 2  | Carl     |
-----------------

So to your question, you appear to want to change the default order by which your table displays this information. In order to do that, check to see what your Primary Key field is. For most practical purposes, having a unique identifying natural number tends to do the trick. MySQL has an AUTO_INCREMENT function for this. When creating the table, it would look something like field_name int NOT NULL AUTO_INCREMENT.

All of this is to say: if you would like to change "the row order", you would need to update this value. However, since the identifier is something that other tables would use to reference your field, this seems a little bit reckless.

If you for example went: UPDATE table Set id = 1 where id = 2;, this would initially fail, since the id fields would end up being both an identical value and fail the PrimaryKey check (which insists on both uniqueness and having a value set). You could Juggle this by running three update statements in a row:

UPDATE users Set id = 100000000 where id = 1;
UPDATE users Set id = 1 where id = 2;
UPDATE users Set id = 2 where id = 100000000;

This would result in the rows for this table looking like:

+----------------
| id | name     |
-----------------
| 1  | Carl     |
| 2  | Brian    |
| 3  | Albert   |
----------------+

Which technically would work to reorder this table, but this is in a bubble. MySQL being a relational database means that any table which was depending on that data to be consistent will now be pointed to the wrong data. For example, I have a table which stores birthdays, referencing the initial user table. It's structure might look like this:

+----------------------------+
| id | user_id  | birthdate  |
+----------------------------+
| 1  | 1        | 1993-01-01 |
| 1  | 2        | 1980-02-03 |
| 1  | 3        | 1955-01-01 |
+----------------------------+

By switching the ID's on the user table, you MUST update the user_id value on the birthdays table. Of course MySQL comes prepared for this: enter "Foreign Key Constraints". As long as you configured all of your foreign key constraints to Cascade Updates, you wouldn't need to manually change the reference to every value you changed.

These queries would all be a lot of manual work and potentially weaken your data's integrity. If you have fields you would like to rank and reorder regularly, the answer posed by Mike Lewis on this question with the "table order" would be a more sensible answer (and if that is the case, then his is the best solution and just disregard this answer).

Clave answered 11/7, 2019 at 1:18 Comment(0)
M
1

I guess a simple order by would be what you're looking for?

select my_column from my_table order by my_order_column;
Marrero answered 1/5, 2009 at 18:38 Comment(0)
O
1

In response to your post here, the answer you may be looking for is:

To order chronologically, add a DateAdded or similar column with a datetime or smalldatetime datatype.

On all methods that insert into the database, make sure you insert CURRENT_TIMESTAMP in the DateAdded column.

On methods that query the database, add ORDER BY DateAdded at the end of the query string.

NEVER rely on the physical position in the database system. It may work MOST of the time but definitely not ALL of the time.

Owen answered 1/5, 2009 at 19:20 Comment(0)
L
1

The question lacks any detail that would let anyone give you correct answer. Clearly you could read the records into memory and then update them. But this is bad on so many different levels.

The issue is like this. Depending on the schema that is actually implemented there is logic to the way that the records are physically written to disk. Sometimes they are written in order of insert and other times they are inserted with space between blocks (see extents).

So changing the physical order is not likely without swapping column data; and this has a deep effect on the various indices. You are left having to change the logical order.

As I read your update... I'm left to understand that you may have multiple users and each user is to have bookmarks that they want ordered. Looks like you need a second table that acts as an intersection between the user and the bookmark. Then all you need is an inner join and an order by.

But there is not enough information to offer a complete solution.

Lustig answered 1/5, 2009 at 22:4 Comment(0)
S
1

Here is a stored procedure script to increment or decrement (one at a time) in MySQL.

Note, MySQL doesn't allow you to select in the same query you're updating so the above answers don't work.

I have also set it to return an error if there is no item above / below if you're incrementing / decrementing, respectively.

DELIMITER $$
CREATE PROCEDURE `spReorderSequenceItems` (
  IN _SequenceItemId INT,
  IN _SequenceId INT,
  IN IncrementUp TINYINT,
  OUT Error VARCHAR(255)
)

BEGIN
  DECLARE CurrentPosition INT;

  SELECT Position INTO CurrentPosition
  FROM tblSequenceItems
  WHERE SequenceItemId = _SequenceItemId;

  IF IncrementUp = 1 THEN
    IF (
      SELECT Position
      FROM tblSequenceItems 
      WHERE Position = CurrentPosition + 1 AND SequenceId = _SequenceId
    ) THEN
      UPDATE tblSequenceItems
        SET Position = Position - 1
        WHERE Position = CurrentPosition + 1 AND SequenceId = _SequenceId;
      UPDATE tblSequenceItems
        SET Position = Position + 1
        WHERE SequenceItemId = _SequenceItemId;
    ELSE 
      SELECT 'No Item Above' AS _Error INTO Error;
    END IF;
  ELSE
    IF (
      SELECT Position
      FROM tblSequenceItems 
      WHERE Position = CurrentPosition - 1 AND SequenceId = _SequenceId
    ) THEN
      UPDATE tblSequenceItems
        SET Position = Position + 1
        WHERE Position = CurrentPosition - 1 AND SequenceId = _SequenceId;
      UPDATE tblSequenceItems
        SET Position = Position - 1
        WHERE SequenceItemId = _SequenceItemId;
    ELSE
      SELECT 'No Item Below' AS _Error INTO Error;
    END IF;
  END IF;
END
$$
DELIMITER ;

Call it with

CALL spReorderSequenceItems(1, 1, 1, @Error);
SELECT @Error;
Surbased answered 10/7, 2019 at 23:17 Comment(0)
C
0

The resorting issue can be executed pretty simply. Once you've added a sort field to your table and you know the sort value of the bookmark you want to move you can do it this way. (Connecting to your DB and running queries in your preferred fashion)

<?php // moving the bookmark up
$original_sort = 3;
$newsort = ($original_sort -1);

$query = "UPDATE bookmarks
SET sort = CASE sort
    WHEN $newsort THEN $original_sort
    WHEN $original_sort THEN $newsort
    ELSE sort
END
WHERE sort IN ($newsort, $original_sort);";

To apply this to a user's preference you will need to create what I call a connector table or one that you use in a LEFT JOIN statement.

CREATE TABLE `user_bookmarks`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `bookmark_id` int NULL DEFAULT 0,
  `sort` smallint NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  INDEX `idx_bookmark_id`(`bookmark_id`) USING BTREE
);

Now run the JOIN query to resort bookmarks for that user.

$query = "UPDATE bookmarks AS b
LEFT JOIN user_bookmarks AS u
   ON b.id = u.bookmark_id
SET u.sort = CASE u.sort
    WHEN $newsort THEN $original_sort
    WHEN $original_sort THEN $newsort
    ELSE u.sort
END
WHERE u.sort IN ($newsort, $original_sort);";

Cobblestone answered 21/3, 2023 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.