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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:52:54+00:00 2026-06-01T02:52:54+00:00

I am trying to insert data into a table in sorted order, for later

  • 0

I am trying to insert data into a table in sorted order, for later fast retrieval. I am using an ordinal column to specify the order of the data. Like so:

SET @ctr = -1;

insert into search_data (trans_id, ordinal)
select trans_id, @ctr:=@ctr+1 
from transactions
order by created;

created is a datetime field.

Doing the select without the insert has the rows coming back in the correct order, but the ctr variable does not increment correctly. E.g.:

+---+----------+--------------+---------------------+
| 1 | trans_id | @ctr:=@ctr+1 | created             |
+---+----------+--------------+---------------------+
| 1 |   131379 |          232 | 2011-10-17 12:27:09 |
| 1 |   131377 |          231 | 2011-10-17 12:24:30 |
| 1 |   131311 |          230 | 2011-10-16 23:44:12 |
| 1 |   131305 |          229 | 2011-10-16 21:57:35 |
| 1 |   129948 |           46 | 2011-10-10 13:24:58 |
| 1 |   129947 |           45 | 2011-10-10 13:24:58 |
| 1 |   129946 |           44 | 2011-10-10 13:24:58 |
| 1 |   129945 |           43 | 2011-10-10 13:24:58 |
| 1 |   129944 |           42 | 2011-10-10 13:24:58 |

This technique has worked for me in MySQL 5.0, 4.x and 3.x. But it doesn’t work in 5.1.

It seems like the sort is being done after the variable is incremented, whereas previously the variable was incremented after the sort

Any thoughts?

  • 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-01T02:52:56+00:00Added an answer on June 1, 2026 at 2:52 am

    Try subquery it:

    select trans_id, @ctr:=@ctr+1
      from ( select trans_id
               from transactions
               order by created, trans_id ) as t
    

    asdfasdf

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

Sidebar

Related Questions

I am trying to insert data into a PostgreSQL database table using Python. I
I am trying to insert some text data into a table in SQL Server
I'm trying to insert data into the SQLite database using get EditText, but I
I'm trying to insert some binary data into database using 'mysql' gem in ruby.
Im trying to insert data into a table in MySQL. I found/modified some code
I am trying to insert data from one of my existing table into another
I am trying to insert a data into SQLite database using Python. INSERT INTO
I am just trying to insert my xml data into a table through calling
I'm trying to insert non-latin data into sqlite database using bind variables using System.Data.SQLite.
Ok so I'm trying to insert data into the customers table and I keep

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.