Need help with SQL query

Discussion in 'Databases' started by sllik, Nov 15, 2007.

  1. #1
    I got this query that gets the position of the playing based on his time (the person with the smallest time is in the first position):
    
    SELECT COUNT( * ) AS position
    FROM (
    
    	SELECT *
    	FROM (
    
    		SELECT *
    		FROM game
    		ORDER BY time
    	)g
    	WHERE time < (
    		SELECT time
    		FROM game
    		WHERE uid =2
    		ORDER BY time
    		LIMIT 1 
    		)
    	GROUP BY uid
    )rank
    
    Code (markup):
    What if I want to go through the list of all users and display their positions.... how would I do that...??
     
    sllik, Nov 15, 2007 IP
  2. rooneydavid

    rooneydavid Guest

    Messages:
    67
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SELECT * FROM game ORDER BY time ASC;

    The above will list everyone in the game, from the top time to the bottom.

    Now to show there position, just add a loop that as each row of data is written it incraments a counter and this is your player position. Example: i = i + 1
     
    rooneydavid, Nov 15, 2007 IP
  3. sllik

    sllik Active Member

    Messages:
    419
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thanks for the quick reply, but the problem is that I have several entries for each user (an entry for every time) and I want to get positions of a set of like 50 specific users out of everyone
     
    sllik, Nov 15, 2007 IP
  4. rooneydavid

    rooneydavid Guest

    Messages:
    67
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ooo thats easy.....

    For them GROUPed do this..
    SELECT * FROM game ORDER BY time GROUP BY uid ASC;
     
    rooneydavid, Nov 15, 2007 IP
  5. sllik

    sllik Active Member

    Messages:
    419
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    again thanks for the quick reply, but I need to get a subset of like 50 users (I have their uids) with their positions compared to everyone else (not just to themselves)
     
    sllik, Nov 15, 2007 IP
  6. rooneydavid

    rooneydavid Guest

    Messages:
    67
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thats tricky stuff, you would have to give out alot more information that what you have already. Is the UIDs 1 - 50 or random?

    To only have 50 records returned use LIMIT(0,50) or SELECT TOP(50) FROM....
     
    rooneydavid, Nov 15, 2007 IP
  7. msaqibansari

    msaqibansari Peon

    Messages:
    84
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    sorry I did not understand your problem. Please define more.
     
    msaqibansari, Nov 15, 2007 IP