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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:30:15+00:00 2026-05-11T20:30:15+00:00

Well this is going to sound like a lame question, I know. This is

  • 0

Well this is going to sound like a lame question, I know. This is probably one of the most obvious things to do, but I’ve been trying to come up with a good way of sorting entries in database.

Image table looking like this:

entry_id   |   entry_data
-------------------------
1          | data1
2          | data2
...        | ...
n          | datan

entry_id is of course Primary Key with AI option so all entries have a unique id.
Now. I want to order that data. Say I want entry_id = 2 to be first without changing its entry_id. Then the table needs another column to store order number.
I tried 2 approaches.

  1. entry_order_no: this will basically keep order number of the items. Any new item will be always added to the end.

  2. left_id + right_id: this approach is used by phpBB. Not sure why it requires right_id though?

I seem to prefer the 1st approach, but how do you sort the data later.
Let’s assume I want to newly added item move from the last position, to say 2nd.
Way I’ve been doing it I was using indexed tables, where index was order no. Each index contained associative array, that had entry_id. That way I “simply” did foreach loop with an UPDATE on all entries from 2nd place down. That may work for few rows. Maybe even tens. But what when you have hundreds? This seems highly inefficient then. Second one seems a bit better. But something still tells me there’s a better approach.

Please do suggest.

  • 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-11T20:30:15+00:00Added an answer on May 11, 2026 at 8:30 pm

    I’m not sure the true nature of why you’re trying to do this, so it’s kind of hard to say, but as far as your inefficient foreach loop, stop using iterative methods on sets – use set operations, they’re way faster and exactly what a DB was meant for. so instead of…

    for row in db>2
        entryorderno += 1
    

    do this…

    UPDATE Table
    SET entryorderno = entryorderno + 1
    WHERE entryorderno > 2
    

    Also, pulling from what Alex Martelli said, doing that should be pretty ok for awhile, but eventually you’ll have to restack everything with new spacing….but a clustered index on that field would keep things in order by that ID….of course it also means inserts to the middle of the table could be resource intensive if it’s a large table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is going to sound like a basic question but... How do I use
I know this is going to sound like a nit-picky question that isn't very
I know this is going to sound silly but I can't find what's wrong.
This is going to sound like an odd request, but... I have a web
Yes, this sounds like a simple question, and it probably is. But when i
First, I don't know where to ask this question. Well this is programming QA
I am asking this question because I know there are a lot of well-read
Well this is a simple question i want to filter two elements sorted reverse
I've recently started tinkering with ASP.NET MVC, but this question should apply to classic
Ok! This sounds like a Let Me Google That For You, well I did

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.