jaymc Posted December 22, 2008 Share Posted December 22, 2008 I have a table which contains 2 fields, username & score I want to find my score and the amount of people that have a better score than me, which will essentually tell me where I am ranked Can this be done in one query? Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/ Share on other sites More sharing options...
Maq Posted December 22, 2008 Share Posted December 22, 2008 SELECT username FROM table WHERE score > (SELECT score FROM table WHERE username = '$username'); Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721077 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 SELECT username FROM table WHERE score > (SELECT score FROM table WHERE username = '$username'); That will pull out all records with a better score than me, but it does not return my own score which I need it to Can you expand? I tried this SELECT count(*), (SELECT minutes_online_month FROM cache WHERE username = 'jaymc') as score FROM cache WHERE minutes_online_month > score But it doesnt like the > score part and I dont want to have 3 queries in one.. Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721506 Share on other sites More sharing options...
fenway Posted December 22, 2008 Share Posted December 22, 2008 Switch to HAVING, not WHERE. Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721561 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 Hmm, not to sure how to use that. I tried SELECT count(*), (SELECT minutes_online_month FROM cache WHERE username = 'jaymc') as score FROM cache HAVING score < minutes_online_month Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721586 Share on other sites More sharing options...
fenway Posted December 22, 2008 Share Posted December 22, 2008 Wait a sec... won't score always be equal to minutes_online_month in your example above? Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721591 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 score will = my own personal minutes_online_month in the table cache WHERE minutes_online_month > score will be everyone elses minutes_online_month in the table Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721604 Share on other sites More sharing options...
fenway Posted December 22, 2008 Share Posted December 22, 2008 Hmm. How about: SELECT count(*), yourScore INNER JOIN (SELECT minutes_online_month FROM cache WHERE username = 'jaymc') as yourScore FROM cache WHERE minutes_online_month > yourScore Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721613 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 syntax error near as yourScore Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721617 Share on other sites More sharing options...
fenway Posted December 22, 2008 Share Posted December 22, 2008 Sorry, my bad, multi-tasking: SELECT count(*), c2.yourScore FROM cache AS c1 CROSS JOIN (SELECT minutes_online_month AS yourScore FROM cache WHERE username = 'jaymc') as c2 WHERE c1.minutes_online_month > c2.yourScore Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721646 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 fenway strikes again Thanks, works perfect. Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721650 Share on other sites More sharing options...
fenway Posted December 22, 2008 Share Posted December 22, 2008 fenway strikes again Thanks, works perfect. No problem.. don't know what I was thinking before; inner join before FROM? I need more sleep ;-) Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721655 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.