Welcome to Dream.In.Code
Become an Expert!

Join 149,501 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,382 people online right now. Registration is fast and FREE... Join Now!




MySQL: Retrieve row position in returned values

 
Reply to this topicStart new topic

MySQL: Retrieve row position in returned values

girasquid
4 Apr, 2007 - 11:03 AM
Post #1

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
Hello, all.

I am currently working on a browsergame where users are ranked against each other based on the size of their armies. The query below returns all user ID's and user names, in the proper order.
CODE

SELECT id, username FROM user_stats ORDER BY (SELECT SUM(unit_amount) FROM user_armies WHERE commander = user_stats.id) DESC LIMIT ?,?

This is all well and good, but I am looking for a way to retrieve exactly what position in the retrieved results a user might have, in order to get their user-specific rank. Does anyone know of a way that I could do this?

Thanks,
Girasquid
User is offlineProfile CardPM
+Quote Post

snoj
RE: MySQL: Retrieve Row Position In Returned Values
4 Apr, 2007 - 11:13 AM
Post #2

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
You can do the same thing you're doing for the ORDER BY in the SELECT part of the "root" query.

SQL
SELECT something, (SELECT aField FROM someTable2 WHERE whatever) AS aName FROM someTable1

User is offlineProfile CardPM
+Quote Post

girasquid
RE: MySQL: Retrieve Row Position In Returned Values
4 Apr, 2007 - 02:23 PM
Post #3

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
While that query does retrieve the user's army size for me, it's not quite what I'm looking for. I need to find out how many users have a larger army than a specific user.
User is offlineProfile CardPM
+Quote Post

snoj
RE: MySQL: Retrieve Row Position In Returned Values
4 Apr, 2007 - 02:38 PM
Post #4

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
Well the awesome thing about nested queries is that you can use variables outside of the query you are in.

SQL
SELECT something1, (SELECT COUNT(t2.userid) FROM aTable1 AS t2 WHERE 1 HAVING t2.armysize > t1.armysize) AS rank FROM aTable1 AS t1


Also, read up on SQL Subqueries http://dev.mysql.com/doc/refman/5.0/en/subqueries.html. It has all the info you'll need to accomplish your task.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month