Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:07:38+00:00 2026-05-27T14:07:38+00:00

I have a table like so: categoryID categoryName —————————- 1 A 2 B 3

  • 0

I have a table like so:

categoryID      categoryName
----------------------------
     1            A
     2            B
     3            C

Now I want the user to be able to order this data according to his will. I want to remember his preferred order for future. So I thought I’d add a column order to the table above and make it of type INT and AUTO_INCREMENT. So now I get a table like this:

categoryID      categoryName    order
-------------------------------------
     1            A               1
     2            B               2
     3            C               3
     4            D               4

My problem is – the user now decides, to bring categoryName with order 4 (D in example above) up to 2 (above B in example above) such that the table would now look like:

categoryID      categoryName    order
-------------------------------------
     1            A               1
     2            B               3
     3            C               4
     4            D               2

My question is – How should I go about assigning new values to the order column when a reordering happens. Is there a way to do this without updating all rows in the table?

One approach that comes to mind is to make the column a FLOAT and give it an order of 1.5 if I want to bring it between columns with order 1,2. In this case I keep loosing precision as I reorder items.

EDIT:
Another is to update all rows between (m, n) where m, n are the source and destination orders respectively. But this would mean running (m-n) separate queries wouldn’t it?

Edit 2:
Assuming I take the FLOAT approach, I came up with this sql to compute the order value for an item that needs to be inserted after item with id = 2 (for example).

select ((
    select `order` as nextHighestOrder
    from `categories`
    where `order` > (
        select `order` as targetOrder 
        from `categories` 
        where `categoryID`=2) 
        limit 1) + (
            select `order` as targetOrder 
            from `categories` 
            where `categoryID`=2)) / 2;

This gives me 3.5 which is what I wanted to achieve.

Is there a better way to write this? Notice that select order as targetOrder from categories where categoryID=9 is executed twice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:07:39+00:00Added an answer on May 27, 2026 at 2:07 pm

    If the number of changes is rather small you can generate a clumsy but rather efficient UPDATE statement if the you know the ids of the involved items:

    UPDATE categories
    JOIN (
        SELECT 2 as categoryID, 3 as new_order
        UNION ALL
        SELECT 3 as categoryID, 4 as new_order
        UNION ALL
        SELECT 4 as categoryID, 2 as new_order) orders
    USING (categoryId)
    SET `order` = new_order;
    

    or (which I like less):

    UPDATE categories
    SET `order` = ELT (FIND_IN_SET (categoryID, '2,3,4'),
                       3, 4, 2)
    WHERE categoryID in (2,3,4);
    

    UPD:

    Assuming that you know the current id of the category (or its name), its old position, and its new position you can use the following query for moving a category down the list (for moving up you will have to change the between condition and new_rank computation to rank+1):

    SET @id:=2, @cur_rank:=2, @new_rank:=4;
    
    UPDATE t1
    JOIN (
      SELECT categoryID, (rank - 1) as new_rank
      FROM t1
      WHERE rank between @cur_rank + 1 AND @new_rank
      UNION ALL
      SELECT @id as categoryID, @new_rank as new_rank
    ) as r
    USING (categoryID)
    SET rank = new_rank;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
I have a table like this: name code group john 12 smith 15 how
I have a table structure like below : categoryID bigint , primary key ,
I have a Table Category, 1) Id 2) CategoryName 3) CategoryMaster with data as:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.