SQL grouping ordered recordsets - mysql

I'm pulling listings from a mySQL database for a high scores table;
SELECT playerID, score FROM leaderboards ORDER BY score DESC
Often the same playerID comes up several times if they've had multiple score submissions.
Is there a way to edit my query to only show that users highest score and ignore following ones by him?

So you wanna get a list of player with their highest score?
SELECT playerID, MAX(score) as max_score
FROM leaderboards
GROUP BY playerID
ORDER BY max_score DESC

SELECT playerID, max(score) as max_score
FROM leaderboards
GROUP BY playerID
ORDER BY max_score DESC

Related

Get top players from the MySQL table

I have table in MySQL DB which contains among other things two fields user_id and score. This table is kind of log table so there can be multiple rows for one user_id with different scores. How can I get only top 10 users with highest score from this table?
SELECT DISTINCT user_id
FROM your_table
ORDER BY score DESC
LIMIT 10
EDIT:
SELECT DISTINCT *
FROM your_table
WHERE (user_id, score) IN (SELECT user_id, MAX(score) AS score
FROM your_table
GROUP BY user_id)
ORDER BY score DESC
LIMIT 10
SqlFiddleDemo
This is basic and you should put more effort; here is atemplate you can use -
SELECT TOP 10 distinct *
FROM people
WHERE names='SMITH'
ORDER BY names asc

MySQL ranking feature

I am trying to create a ranking system using the data in my table. It has 2 columns, name and score. One user can have multiple scores. This is what I have so far:
("SELECT name, score, FIND_IN_SET( score, (SELECT GROUP_CONCAT ( score ORDER BY score DESC ) FROM data )) AS rank FROM scores");
But this command lists all the user's previous scores when I only need the highest score.
Does anyone know how I can achieve this?
Try
SELECT name, sum(score) from scores group by name order by sum(score) desc limit 1
And if you don't want to sum then use
SELECT name, max(score) from scores
The below query will pop the list of name in the order of highest score respectively.
select name, max(score) as score from scores group by name order by score desc
You can use SELECT DISTINCT name, score ...
and Select TOP 1 and ORDER BY

mySQL count and get maximum from table

I have a simple question. I have this table structure http://sqlfiddle.com/#!2/61cee/3/0 and I just want to count score of each player which are attacker and then pick the highest score from all players and his id. I tried this:
SELECT MAX(counts), id_player FROM
(SELECT COUNT(score) AS counts, id_player
FROM fl_poll
WHERE position = 'attacker'
GROUP BY id_player) as maxi
But the result is wrong id. Where I made a mistake? Thanks for advance.
SELECT COUNT(score) AS counts, id_player
FROM fl_poll
WHERE position = 'attacker'
GROUP BY id_player
ORDER BY COUNT(score) desc
LIMIT 1

MySQL subquery for values in ORDER BY FIELD

I'm trying to write a query which will get the maximum scores for the most popular songs, in order of song popularity.
I have a table called "gameplay" with the following fields:
id
song_id
score
(plus some other arbitrary fields)
So far, the closest I've got is this:
SELECT id, song_id, score
FROM (
SELECT id, song_id, score
FROM (
SELECT id, song_id, score
FROM gameplay
ORDER BY score DESC
) AS all_scores_in_order
GROUP BY song_id
) AS top_scores_per_song
ORDER BY FIELD (song_id, 3,1,2)
But I would like the values in ORDER BY FIELD to be generated by another subquery - the song_id ranked by popularity (in order of counting row occurrences in table) ie.
SELECT song_id
FROM gameplay
GROUP BY song_id
ORDER BY count( id ) DESC
Can't you just group and then order by Count(id) in the outer query?
This should show all sorts sorted by the number of occurrences, and showing the max score:
SELECT song_id, COUNT(id), MAX(score)
FROM gameplay
GROUP BY song_id
ORDER count(id) DESC
Or do you want each song_id to appear several times?

Selecting values grouped to a specific identifer

I have an application that tracks high scores in a game.
I have a user_scores table that maps a user_id to a score.
I need to return the 5 highest scores, but only 1 high score for any specific user.
So if user X has the 5 highest scores on a purely numerical basis, I simply return the highest one and then the next 4 user scores.
I have tried to use:
SELECT user_id, score
FROM user_scores
ORDER BY score DESC
GROUP BY user_id
LIMIT 5
But it seems that MySQL drops any user_id with more than 1 score.
This should work:
SELECT user_id, MAX(score)
FROM user_scores
GROUP BY user_id
ORDER BY MAX(score) DESC
LIMIT 5
SELECT user_id, MAX(score) AS score
FROM user_scores
GROUP BY user_id
ORDER BY score DESC
LIMIT 5
Should do the job for you... though don't forget to create indexes...
You can't group by without a summary-function (SUM, COUNT, etc.)
The GROUP BY clause says how to group the SUMs or COUNTs.
If you simply want to break the long list into bunches with a common value, that's not SQL. That's what your application has to do.
Can you use the Distinct operator to say
SELECT DISTINCT(user_id), score
FROM user_scores
ORDER BY score DESC
LIMIT 5
didn't test so not sure if that will definitely work
Returning only the maximum score for a given user is something like the following.
SELECT user_id, max(score) FROM user_scores
GROUP BY user_id
I don't know whether it was a lack of caffeine or just brain explosion, but the answers here were so easy.
I actually got it working with this monstrosity:
SELECT s1.user_id,
(SELECT score FROM user_scores s2 WHERE s2.user_id = s1.user_id ORDER BY score DESC LIMIT 1) AS score
FROM user_scores s1
GROUP BY s1.user_id
ORDER BY s1.score DESC
LIMIT 5