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? Quote 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'); Quote 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.. Quote 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. Quote 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 Quote 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? Quote 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 Quote 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 Quote 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 Quote 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 Quote 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. Quote 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 ;-) Quote Link to comment https://forums.phpfreaks.com/topic/137952-solved-my-rank/#findComment-721655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.