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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:36:48+00:00 2026-05-30T10:36:48+00:00

I have a MySQL table that looks like this: id (int primary) name (text)

  • 0

I have a MySQL table that looks like this:

id (int primary)
name  (text)
rating (float)

I have a page showing rankings which looks like this:

$i = 0;
$q = mysql_query("SELECT * FROM teams ORDER BY rating DESC");
while($r = mysql_fetch_assoc($q)){
    $i++;
    print("$i: {$r['name']}<br>");
}

This shows teams in order of their rating, with a ranking. And it works.

Now, if I’m given the ID of a team, how do I find their ranking without running through the loop like this? A single MySQL query which returns the team’s info + a numeric ranking indicating how far down the list they would be, if I had rendered the whole list.

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-05-30T10:36:49+00:00Added an answer on May 30, 2026 at 10:36 am

    To get the ranking you can do:

    SELECT COUNT(*) as ranking
    FROM teams t
    WHERE t.rating >= (SELECT rating FROM teams WHERE id=$ID);
    

    To get all the relevant info too, you can do:

    SELECT t.*,COUNT(*) as rank
    FROM teams t
    JOIN teams t2 ON t.rating<=t2.rating
    WHERE t.id=4;
    

    This joins teams to itself joining on t.rating <= t2.rating, and so you get one row for every team that has a rating higher than or equal you.

    The COUNT just counts how many teams have a rating higher than or equal to you.

    Note that if there’s a tie this will give you the lower rank. You can change the <= to a < if you want the highest.

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

Sidebar

Related Questions

Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
I have a mysql table that looks something like this: Row 1: 'visitor_input_id' =>
I have a table in a MSSQL database that looks like this: Timestamp (datetime)
I have a table in MySQL That looks like the following: date |storenum |views
Let's say I have a MySQL table that is something like this: software table:
I have a table that looks like this CREATE TABLE IF NOT EXISTS `language`
Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
I have a MySQL InnoDB table laid out like so: id (int), run_id (int),
Using MySQL 5 I have a table like this: date (varchar) door (varchar) shift
I have a table that contains company names in this format: Name Name ,

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.