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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:12:34+00:00 2026-06-10T08:12:34+00:00

From MySQL – Get row number on select I know how to get the

  • 0

From MySQL – Get row number on select
I know how to get the row number / rank using this mysql query:

SELECT @rn:=@rn+1 AS rank, itemID
FROM (
  SELECT itemID
  FROM orders
  ORDER BY somecriteria DESC
) t1, (SELECT @rn:=0) t2;

The result returns something like this:

+--------+------+
| rank | itemID |
+--------+------+
|  1   |   265  |
|  2   |   135  |
|  3   |   36   |
|  4   |   145  |
|  5   |   123  |
|  6   |   342  |
|  7   |   111  |
+--------+------+

My question is: How can I get the result in 1 simple SINGLE QUERY that returns items having lower rank than itemID of 145, i.e.:

+--------+------+
| rank | itemID |
+--------+------+ 
|  5   |   123  |
|  6   |   345  |
|  7   |   111  |
+--------+------+

Oracle sql query is also welcomed. Thanks.

  • 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-10T08:12:36+00:00Added an answer on June 10, 2026 at 8:12 am

    An Oracle solution (not sure if it meets your criteria of “one simple single query”):

    WITH t AS
    (SELECT item_id, row_number() OVER (ORDER BY some_criteria DESC) rn
       FROM orders)
    SELECT t2.rn, t2.item_id
      FROM t t1 JOIN t t2 ON (t2.rn > t1.rn)
     WHERE t1.item_id = 145;
    

    My assumption is no repeating values of item_id.

    Attempting to put this in MySQL terms, perhaps something like this might work:

    SELECT t2.rank, t2.itemID
      FROM (SELECT @rn:=@rn+1 AS rank, itemID
              FROM (SELECT itemID
                      FROM orders
                     ORDER BY somecriteria DESC), (SELECT @rn:=0)) t1 INNER JOIN
           (SELECT @rn:=@rn+1 AS rank, itemID
              FROM (SELECT itemID
                      FROM orders
                     ORDER BY somecriteria DESC), (SELECT @rn:=0)) t2 ON t2.rank > t1.rank
     WHERE t1.itemID = 145;
    

    Disclaimer: I don’t work with MySQL much, and it’s untested. The Oracle piece works.

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

Sidebar

Related Questions

I am getting elements from MySQL by using this query; select * from bahis
If I run the following query: select * from mysql.user order by abcdef; MySQL
I need to reverse result from mysql query, but i cannot use ORDER BY
I need select result from mysql regarding query string. Let's string will be: Z
I'm using the MySql.Data dll from MySql and trying to execute a query to
Exporting select query data from mysql to csv & opening with excel, the leading
From MySQL query I get data which I put into a dictionary d: d
I can get data from MySql with use of php using android successfully. But
Exporting some data from mysql to a csv file using FasterCSV. I'd like the
How get data from mysql database? I use ASP.NET and C#.Previously, I used LINQ

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.