Mailing List
Home
Forum Home
MySQL General - General MySQL discussion
MaxDB - Everything about MaxDB, formerly known as SAP DB
MySQL++ - Programming with the C++ API to MySQL
MySQL on Win32 - Runing MySQL on Windows 9x/Me/NT/2000/XP
ODBC - ODBC with the MySQL Connector/ODBC driver
Java Help - Mostly related to the MySQL Connector/J driver
Perl - Perl support for MySQL with DBI and DBD::mysql
GUI - MySQL GUI Tools
Announcement
Subjects
mysql openssl Question
mysql:it 's a db not a dbms how it 's possible?!
Does the binary log enabling affect the MySQL performances?
Strange behavior, Table Level Permission
FULLTEXT query format question
Preventing Duplicate Entries
Comparing and writing out BLOBS
Executing MySQL Commands From Within C Program
Can 't access mysql after kernel upgrade
Mysql 4 0 Oracle Stored Procedure Trigger Conversion
Downgrade Mysql from 4 to 3 23
MySQL Cluster Software
mysql test 4 1 fails with the gis test
ERROR 2002: Can 't connect to local MySQL server through socket
Getting Identity after INSERT
Update one field with more fields from another table
ERROR 1045: Access denied for user: 'root@localhost ' (Using
password: NO)
mysql have same function mthod as Oracle decode()
 
Help with query: Row number / Rank from a query...

Help with query: Row number / Rank from a query...

2006-06-01       - By Michael Stassen

 Back
Reply:     1     2     3     4  

Dan wrote:
> I have a regular query lets say:

Better to show the real query, rather than a simplified version.  Simplified
requests get you simplified answers.  What seems like a simplification to you,
may in fact hide a crucial piece of information, thus preventing anyone from
giving you a solution to your real problem.  We need the real query to give a
real answer.

> SELECT user, points FROM results ORDER BY points DESC
>
> so I get:
>
> user    points
> -- ---- ---- --
> john    23
> steve   17
> bill    14
>
>  From this I want to get a particular users 'rank' or row number from  
> the query.  I was hoping for a single line elegant query for this.
>
> I thought of this:
>
> SET @(protected) = O;
> SELECT @(protected):= @(protected) + 1 as row, SUM(IF(user = 'steve',@(protected),0)) as rank, user  
> FROM points ORDER BY points DESC GROUP BY id;

That's not your real query, as the table name seems to have changed from
"results" to "points".

> but I get:
>
> rank    user
> -- ---- ---- --
> 0    john
> 2    steve
> 0    bill

I don't think so.  Your select has a column named row, but it's missing in your
output.

From your description of the problem, I cannot tell why you made the second
query so complicated.  What is wrong with the following, simpler query?

  SET @(protected) = 0;

  SELECT @(protected):= @(protected) + 1 AS rank, user
  FROM results
  ORDER BY points DESC;

It should give

  rank    user
  -- ---- ---- --
  1       john
  2       steve
  3       bill

as the results.  Isn't that what you want?

> which does not really help me
>
> Any ideas?  I tried the manual, Google and the list with no sucsess.
>
> Thanks,
>
> Dan T

If you show us what you want, instead of something that isn't what you want,
someone may be able to tell you how to get it.

Michael


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=mysql@(protected)