most simple “return highscores” SQL Query using userID

笑着哭i 提交于 2019-12-23 20:26:37

问题


High there

working at a minimal game on Android I get lost when it comes to sql queries.

I'm trying to get a top 100 ranking list and it almost works but not quite

I am using this query

$sql = "SELECT * FROM WorldGames_table GROUP BY user_id ORDER BY score DESC LIMIT 100";

the thing is that it returns a list of the top 100 scores, in the proper order and containing only 1 score of each user. But it is not always the best score of the users that is shown.

I am getting completely confused by the way complexes queries are "organised", that is why I am looking for a simple query

cheers

UPDATE: Solved (or so I though at the time :s)

this is the line that returns what i was looking for :D

SELECT WorldGames_table .*, MAX(score) as score FROM WorldGames_table GROUP BY id, device_id ORDER BY score DESC LIMIT 100;

Thanks to AT-2016 and to every one who gave a hand :D

UPDATE II: not solved after all :S

So happy that the proper Highscores were showing up in the ranking, I didn't realise that, actually, with this query All the scores from each user are showing up, instead of returning only the best score of each user. Here I go again, in the search of the most simple "return highscores" SQL Query using userID

UPDATE III: I think the answer might come from this place so I put this together, and again, it works in my local test and fails when a apply it on my server

SELECT a.id, a.timestamp, a.name, a.score, a.color, a.flower, a.user_id

FROM games_table a

INNER JOIN (

    SELECT id, MAX(score) score
    FROM games_table
    GROUP BY user_id

) b ON a.id = b.id AND a.score = b.score

ORDER BY a.score DESC

LIMIT 100 

Still investigating, the piñata way ;)

UPDATE IV: I think I have it (but I won't it is done before I'm sur) In the mean time this is what I found. NOPE, ANOTHER FAILED ATTEMPT :(

I tried my queries directly inside PHPmyAdmin

Found out that that query

SELECT
timestamp, name, MAX(score), color, flower, device_id 

FROM
 WorldFlowers_table 

GROUP BY 
 device_id 

ORDER BY
 score 
 DESC 

LIMIT 100;

returned an error message like this: "Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available."

So I added "id," at the beginning of line #2

and now it looks like I get the result I was looking for I did some test but I will make sur it works to mark the question as "answered"

Did not work, now I'm looking at this thread without much hope

The only thing I gained today is that I test the queries directly into phpmyadmin

if there is anyone out there HELP !

UPDATE V: someone called scaisEdge gave me this answer and, at least until now, it seems to work. Waiting for more testing to say "answered"

  select * from WorldFlowers_table
  where  (device_id, score) in (  select device_id, max(score) 
                                  FROM WorldFlowers_table 
                                  GROUP BY device_id  
                                  ORDER BY score DESC 
                                  )
  ORDER BY score DESC 
  LIMIT 100 

UPDATE VI

it works !! :)

and the answer was given to me , like I said above, by scaisEdge, in another "instance" of this question that I posted here mySQL "get TOP 100 scores" query is turning me crazy


回答1:


Try to use MAX() to get the highest values:

SELECT MAX(Values) FROM WorldGames_table 
GROUP BY user_id ORDER BY score DESC LIMIT 100

Try the below:

SELECT *
FROM Table JOIN
    (SELECT MIN(Score) as Values -- Or use MAX()
      FROM (SELECT Score
            FROM Table
            ORDER BY Score
            LIMIT 100
           ) Table2
    ) Table2
ON Table.Score >= Table2;



回答2:


Have you tried to use the sum?

$sql = "SELECT SUM(score) as sum_score FROM WorldGames_table GROUP BY user_id ORDER BY SUM(score) DESC LIMIT 100";



回答3:


I don't know if I understand you well but maybe you need to write your query like that:

$sql = "SELECT WorldGames_table.*, MAX(score) as score FROM WorldGames_table GROUP BY user_id ORDER BY score DESC LIMIT 100";


来源:https://stackoverflow.com/questions/39789596/most-simple-return-highscores-sql-query-using-userid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!