Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Find Position in table

Tags:

sql

mysql

I have a table in mySql which has the users ID and scores.

What I would like to do is organise the table by scores (simple) but then find where a certain user ID sits in the table.

So far I would have:

SELECT * FROM table_score
ORDER BY Score DESC

How would I find where userID = '1234' is (i.e entry 10 of 12)

like image 979
Travis Gandy Avatar asked Jan 14 '13 00:01

Travis Gandy


People also ask

How do I select a position in SQL?

MySQL POSITION() FunctionThe POSITION() function returns the position of the first occurrence of a substring in a string. If the substring is not found within the original string, this function returns 0. This function performs a case-insensitive search. Note: The LOCATE() function is equal to the POSITION() function.

How do I find the position of a character in SQL?

SQL Server CHARINDEX() Function The CHARINDEX() function searches for a substring in a string, and returns the position. If the substring is not found, this function returns 0. Note: This function performs a case-insensitive search.

How do I find the location of a table in SQL Server?

Another easiest method to find the tables by the table's name in SQL Server database is to use the filter settings option in the object explorer in SQL Server Management Studio. In the Object Explorer in SQL Server Management Studio, go to the database and expand it.

How do I find a specific value in a SQL database?

We can use system catalog view sys. objects to view all objects in a SQL database. It has a column type that contains the object category. For example, if we want to search only for the user-defined table, we use 'U' value for the type column.


2 Answers

The following query will give you a new column UserRank, which specify the user rank:

SELECT 
  UserID, 
  Score, 
  (@rownum := @rownum + 1) UserRank 
FROM table_score, (SELECT @rownum := 0) t 
ORDER BY Score DESC;

SQL Fiddle Demo

This will give you something like:

| USERID | SCORE | USERRANK |
-----------------------------
|      4 |   100 |        1 |
|     10 |    70 |        2 |
|      2 |    55 |        3 |
|   1234 |    50 |        4 |
|      1 |    36 |        5 |
|     20 |    33 |        6 |
|      8 |    25 |        7 |

Then you can put this query inside a subquery and filter with a userId to get that user rank. Something like:

SELECT
  t.UserRank
FROM
(
   SELECT *, (@rownum := @rownum + 1) UserRank 
   FROM table_score, (SELECT @rownum := 0) t 
   ORDER BY Score DESC
) t
WHERE userID = '1234';

SQL Fiddle Demo

like image 59
Mahmoud Gamal Avatar answered Sep 17 '22 09:09

Mahmoud Gamal


For a given user id, you can do this with a simple query:

select sum(case when ts.score >= thescore.score then 1 else 0 end) as NumAbove,
       count(*) as Total
from table_score ts cross join
     (select ts.score from table_score ts where userId = '1234') thescore

If you have indexes on score and userid, this will be quite efficient.

like image 43
Gordon Linoff Avatar answered Sep 18 '22 09:09

Gordon Linoff