How can I reorder rows in sql database

后端 未结 14 1827
忘了有多久
忘了有多久 2020-11-28 03:24

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

相关标签:
14条回答
  • 2020-11-28 03:38

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

    select my_column from my_table order by my_order_column;
    
    0 讨论(0)
  • 2020-11-28 03:39

    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:

    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'
    
    0 讨论(0)
  • 2020-11-28 03:47

    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).

    0 讨论(0)
  • 2020-11-28 03:47

    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.

    0 讨论(0)
  • 2020-11-28 03:49

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

    SELECT * FROM User ORDER BY LastName
    
    0 讨论(0)
  • 2020-11-28 03:49

    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;
    
    0 讨论(0)
提交回复
热议问题