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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:09:10+00:00 2026-06-10T02:09:10+00:00

In my Events table I have a column called Pos1, which contains IDs from

  • 0

In my Events table I have a column called Pos1, which contains IDs from A to E. I need to count how many times ‘E’ appears in the column and where it ranks alongside the other numbers.

For example in this table ‘E’ occurs 1 time and is ranked 4 (D=3, A=2, C=2, E=1, B=0).

Pos1
E
C
D
C
D
D
A
A

I’m a complete SQL amateur so the closest I’ve got is an array printing the count of each ID, but I need this to be limited to counting a single ID and printing the rank.

$query = "SELECT Pos1, COUNT(Pos1) FROM Events GROUP BY Pos1"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

    echo $row['COUNT(Pos1)'] ."<br />";

}
  • 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-10T02:09:11+00:00Added an answer on June 10, 2026 at 2:09 am

    try this:

    SELECT Pos1, a.cnt, rank
    FROM(
        SELECT a.Pos1, a.cnt, (@rank := @rank + 1) AS rank
        FROM(
            SELECT Pos1, COUNT(Pos1) cnt
            FROM Events
            GROUP BY Pos1
        ) a, (SELECT @rank := 0) b
        ORDER BY a.cnt DESC
    )a
    WHERE a.pos1 = 'E';
    

    SQLFIDDLE DEMO HERE

    EDIT: php code

    $query = "SELECT Pos1, a.cnt, rank
              FROM(
                SELECT a.Pos1, a.cnt, (@rank := @rank + 1) AS rank
                FROM(
                    SELECT Pos1, COUNT(Pos1) cnt
                    FROM Events
                    GROUP BY Pos1
                ) a, (SELECT @rank := 0) b
                ORDER BY a.cnt DESC
              )a
              WHERE a.pos1 = '$pos1'";
    
    $result = mysql_query($query) or die(mysql_error());
    
    $row = mysql_fetch_row($result);
    
    echo $row['Pos1']." ".$row['cnt']." ".$row['rank']."<br />";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called Event. A column has duration--which is...how long that event
I have an events table with a 'featureEvent' boolean column. I need to display
I have a table called teams with column inactive and another table events with
I have 2 tables events and events_visibility events table and the column id -
I have two tables, events and photos, which relate together via the 'Event_ID' column.
I have a many-to-many relationship between the two entities called events and artists, both
Assuming I have table called events with the columns INT id DATETIME start_time DATETIME
Suppose we have a table Users that has only one column UserId which is
I have a table called Events that I want to insert information to. Here
I have a column in a table called Complete that is a boolean .

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.