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 8857543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:36:54+00:00 2026-06-14T14:36:54+00:00

Let say I have this table, ID Name Order ======== ======== ========= 1 d

  • 0

Let say I have this table,

 ID         Name       Order
======== ========  =========
 1           d          1
 2           g          2
 3           b          3
 4           f          4
 5           a          5

Now, from my UI user can change the order by drag drop. For example, he can move an item d to to item a place. Then my table will look like,

 ID         Name       Order
======== ========  =========
 2           g          1
 3           b          2
 4           f          3
 1           d          4
 5           a          5

Can any one tell me the SQL? I have input of ID, OldOrder and NewOrder.

  • 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-06-14T14:36:56+00:00Added an answer on June 14, 2026 at 2:36 pm

    You don’t even need the ID parameter.

    UPDATE
      yourTable
    SET
      Order = (CASE WHEN     Order = @oldOrder THEN @newOrder
                    WHEN @newOrder > @oldOrder THEN Order - 1
                                               ELSE Order + 1 END)
    WHERE
         Order BETWEEN @oldOrder AND @newOrder
      OR Order BETWEEN @newOrder AND @oldOrder
    
    • Anything above or below the original and new positions don’t need to be changed.
    • Everything else moves up or down one.
    • Except the original item, that moves to the new position.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really simple example: Let's say I have this table: ID Name GUID1 John GUID2
Let's say I have a table like this: name |order_id ======================= first_record | 0
I have this table: id feed_id ... Let's say that I have 500 rows
Let's say I have a table Foo . This table has the columns ID
Let's say I have a MySQL table that is something like this: software table:
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say I have a table like this a | b | c |
Let's say I have this query: select * from table1 r where r.x =
Let's say I have this query, SELECT T.A FROM T WHERE T.A IN (CASE
Let's say I have a model like this: create_table :ninjas do |t| t.string name

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.