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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:36:37+00:00 2026-05-25T06:36:37+00:00

I have a table with two columns [id, value] both numeric. In this example:

  • 0

I have a table with two columns [id, value] both numeric.

In this example:

[ id, value ]
[ 1,  6     ]
[ 2,  4     ]
[ 3,  10    ]
[ 4,  2     ]
[ 5,  7     ]
[ 6,  3     ]

For a given id I’d like to retrieve the top 3 id’s (those with highest value), their top position and if the given id is not in the top 3, also get its position, id and value:

Example 1: ask_id = 5 Return:

[ position, id, value ]
[ 1,        3,  10    ]
[ 2,        5,  7     ]
[ 3,        1,  6     ]

Example 2: ask_id = 4. Return:

[ position, id, value ]
[ 1,        3,  10    ]
[ 2,        5,  7     ]
[ 3,        1,  6     ]
[ 6,        4,  2     ]

So the important points are:

  • How to get for the position column?
  • How to get the additional row if possible (anyway there’s no problem if I need two queries)?
  • 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-25T06:36:38+00:00Added an answer on May 25, 2026 at 6:36 am
    select t2.pos, t1.id, t1.value 
    from test as t1
    inner join
    (select id, value, @pos:=if(@pos is null, 0, @pos)+1 as pos 
     from test order by value desc) as t2
    on t1.id=t2.id
    where t2.pos<=3 or t2.id={$ask_id}
    order by t2.pos;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very simple example: I have a table with two columns, id
I have a table with two columns, GroupId and ParentId (both are GUIDS). The
I have two tables, both with the same columns. The first table contains original
I have two separate columns within the same table in my database. Both of
I have a table that has two columns both of them are continuous data.
I have a table with different columns I am showing two in this code
I have a table in Oracle called quotes with two columns: date and value
I have two columns. when i am dividing values of both table i am
I have a table with two columns and one row, and 100% width across
I have a table with two columns: column A column B 1 2 1

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.