Note that there are some explanatory texts on larger screens.

plurals
  1. POCompletely arbitrary sort order in MySQL with PHP
    primarykey
    data
    text
    <p>I have a table in MySQL that I'm accessing from PHP. For example, let's have a table named THINGS:</p> <blockquote> <p>things.ID - int primary key</p> <p>things.name - varchar</p> <p>things.owner_ID - int for joining with another table</p> </blockquote> <p>My select statement to get what I need might look like:</p> <blockquote> <p>SELECT * FROM things WHERE owner_ID = 99;</p> </blockquote> <p>Pretty straightforward. Now, I'd like users to be able to specify a completely arbitrary order for the items returned from this query. The list will be displayed, they can then click an "up" or "down" button next to a row and have it moved up or down the list, or possibly a drag-and-drop operation to move it to anywhere else. I'd like this order to be saved in the database (same or other table). The custom order would be unique for the set of rows for each owner_ID.</p> <p>I've searched for ways to provide this ordering without luck. I've thought of a few ways to implement this, but help me fill in the final option:</p> <ol> <li>Add an INT column and set it's value to whatever I need to get rows returned in my order. This presents the problem of scanning row-by-row to find the insertion point, and possibly needing to update the preceding/following rows sort column.</li> <li>Having a "next" and "previous" column, implementing a linked list. Once I find my place, I'll just have to update max 2 rows to insert the row. But this requires scanning for the location from row #1.</li> <li>Some SQL/relational DB trick I'm unaware of...</li> </ol> <p>I'm looking for an answer to #3 because it may be out there, who knows. Plus, I'd like to offload as much as I can on the database.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload