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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:45:30+00:00 2026-06-16T05:45:30+00:00

Considering the following table: rowid | text | group 1 | a | 1

  • 0

Considering the following table:

rowid   |   text    |   group
1       |   a       |   1
2       |   b       |   2
3       |   c       |   2
4       |   d       |   3
5       |   e       |   4
6       |   f       |   1
7       |   g       |   1
8       |   h       |   4
9       |   j       |   5
10      |   k       |   2
11      |   i       |   3
12      |   l       |   3

Now imagine I want the row number of rowid 6 (for use in eg. pagination). Since it’s auto incrementing, it’d be very simple to just select where rowid = 6, but in this case I want to know what row number it has in group 1.

Obviously it’s easy to do this by eye in such a small example (it’s row number 2).

Row number 1 would be rowid = 1.
Row number 2 would be rowid = 6.
Row number 3 would be rowid = 7.

My first thought was something like this:

SELECT
    tmp.rowid,
    tmp.text,
    tmp.group,
    tmp.row_number
FROM
    (
        SELECT
            `rowid`,
            `text`,
            `group`,
            (@rowNumber := @rowNumber + 1) as `row_number`
        FROM
            `my_table`,
            (SELECT @rowNumber := 0) as `r`
        WHERE
            `group` = 1
        ORDER BY
            `rowid` DESC
    ) as `tmp`
WHERE
    tmp.rowid = 6

Which will give the right result (in row_number), but on a big table (half a million records) this will take a good 2.5 seconds onn a quick server, as it has to first collect all 500,000 entries, and THEN select row id 6.

What should I do to make this quicker/better?

  • 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-16T05:45:31+00:00Added an answer on June 16, 2026 at 5:45 am

    The solution is actually very simple.

    SQL

    SELECT
        COUNT(`rowid`) as `row_number`
    FROM
        `my_table`
    WHERE
        `group` = 1
    AND
        `rowid` >= 6
    

    NOTE

    You need to have a row with an incrementing value (such as a timestamp, an incrementing ID, etc.).

    Here we convert the original ORDER BY rowid DESC to WHERErowid>= '6'. If you use ORDER BY ascending, you’ll want to use <= instead.

    The query takes 0.00002 seconds, and returns the correct row number (in row_number). If you want the text and group columns, you’ll have to either make this a subquery or make an additional query that selects WHERE rowid = 6.

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

Sidebar

Related Questions

Considering the following table: someId INTEGER #PK ageStart TINYINT(3) ageEnd TINYINT(3) dateBegin INTEGER dateEnd
Considering the following table: create table inttest ( someint INT(10) NULL DEFAULT NULL );
How to store results from following query into another table, considering there is an
In a SQL Server table I have the following 2 columns: RowId : primary
Rather than use an ORM, I am considering the following approach in Python and
Considering the following table: User CreatedDateTime Quantity ----- ----------------- -------- Jim 2012-09-19 01:00 1
Considering the following table: CREATE TABLE `foo`( `aaa` VARCHAR(45) NOT NULL, `bbb` VARCHAR(45) NOT
This isn’t trully a problem, I just want to understand. Considering the following code:
Considering following code public class A { public static void main(String[] args) { new
Considering the following TSQL: INSERT INTO Address(Street1, City, State, ZipCode) SELECT Street1, City, StateCode,

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.