简体   繁体   中英

Best way to change order of rows in MySQL table?

I have a MySQL table with rows that need to be sorted in a particular order decided by the user. In other words, need the user to be able to insert a new row at a random point in the table, and also move existing rows to a new random location/row number in the table output on a select query...

Solutions I have thought of so far:

  1. I could use an integer with an ORDER BY, however, then i will need to update every row after the point of "insertion".

  2. I could use a timestamp and an index integer, set the index integer of the new row to be equal to that of the row currently occupying the point of insertion, and then in the query first order by index, then by timestamp, but the "updating all" problem still occurs when i want to move an old row to a new index, instead of insert a new one.

One of the most fundamental points of relational databases in general is that the order of the data (as stored) is utterly irrelevant.

If you want retrieved data in a particular order, select the data, and specify the required order in an order-by clause.

If you want to specify that order all the time, you might want to create a view, and specify an ordering in the definition of the view (this can still be overridden if you do a select on the view that specifies its own order by clause).

If you really retrieve all the data from that table in a particular order all (or nearly all) the time, you may want to create a clustered index on that order. This can/will (typically) help in retrieving data in that order.

What @Jerry said is true, the sequence of data in a table is not important. Associated or related data is.

For example, if you are trying to record the times of certain events, you need to include a column for that time:

item    time
----    ----
one     01:00
two     02:00
four    04:00

If you were noting this on paper or a whiteboard or something, a medium that also encompasses presentation , you would erase the last row if you wanted to include item "three" at 03:00. But in a database, you can simply insert ( insert is sort of a misnomer) new data to the end of a table:

item    time
----    ----
one     01:00
two     02:00
four    04:00
three   03:00

The storage of data need not be sequential. When you present the data (for human readability or a stockholder report), you then order it according to what suits the presentation best.

What if instead of numbered "items" you had names:

person    time
----      ----
Betty      01:00
Annie      02:00
Charlie    03:00

Here the rows are "in order" by time, but not alphabetically by name. What if a report required you to order data by name?

SELECT name, time
FROM mytable
ORDER BY name ASC;

Output:

Annie      02:00
Betty      01:00
Charlie    03:00

If you need random order:

SELECT name, time
FROM mytable
ORDER BY RAND();

Hopefully this additional info helps, as I think storage and presentation of data are two concepts that you were perhaps tying together.

(A spreadsheet like Excel, for example, in many cases ties together both storage and presentation.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM