MySQL Select the highest from last 50 rows - mysql

I want to select the highest won from my last 50 rows.
But why is that not working?
I found that in other questions but i dont know why that doesnt work for me?
This is my query with that what i tried
SELECT max(won) from games WHERE game ='case' ORDER BY id DESC LIMIT 50
I want the highest won from the last 50 rows by DESC so i order by id DESC.
But than i get the highest won of everything won, not only from the last 50 rows.

You could do the limit first and then get the max.
SELECT max(won) FROM
(
SELECT * FROM games
WHERE game ='case'
ORDER BY id DESC LIMIT 50
) AS tbl

Try with subquery:
select max(won)
from
(SELECT * from games WHERE game ='case' ORDER BY id DESC LIMIT 50)a

Related

How can I select the row from a table whose 1 field is maximum or minimum?

In my table I have a field name rt_stars which includes integer anyone between 1 to 5. Suppose there are 5 rows having rt_stars 4,4,3,2,1 respectively. Here, 4 is the highest and 1 is the lowest. If I want to select the row with the maximum value 4 how can I do that? Since there are two 4 here the last one will be selected. Can I have query something like this?
SELECT * FROM ratings WHERE MAX(rt_stars) ORDER BY rt_id DESC LIMIT 1
I know this is wrong but that's how I want to select all the values from the rows if the rt_stars field has the maximum value in it. How can I achieve this kind of a query?
You can select one row using:
select r.*
from ratings r
order by rt_stars desc, rt_id desc
limit 1;
The problem with your query is that you cannot use max() in the where clause. Beyond that, you don't need aggregation at all -- just ordering the rows and then selecting the first one.
If you want all rows with the maximum stars you can do:
select *
from ratings
where rt_stars = (select max(rt_stars) from ratings)
If you just want one of them randomly you can do:
select *
from ratings
order by rt_stars desc
limit 1
I think the below SQL code will help.
SELECT * FROM ratings WHERE rt_stars = MAX(rt_stars) ORDER BY rt_id DESC;
Sorry the code modified below
SELECT * FROM ratings WHERE rt_stars = ( SELECT MAX( user_05_pk ) FROM ratings )
ORDER BY rt_id DESC;

SQL query for top 100, displayed in reverse

I have a database of logos, 18K strong. Each logo has a score. I want to select the top 100 by score and display them from 100 to 1, not from 1 to 100.
My query is:
SELECT * FROM tbllogos WHERE status = 'live' ORDER BY score DESC LIMIT 100
which works fine for selecting the top 100, but the php WHILE loop then displays them from 1 to 100. I can't figure out how to swap the order so it displays 100 to 1. Changing DESC to ASC obviously isn't the answer as that selects the 100 with the lowest scores.
Use a subquery:
SELECT t.*
FROM (SELECT *
FROM tbllogos
WHERE status = 'live'
ORDER BY score DESC
LIMIT 100
) t
ORDER BY score ASC;

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

Query MSQL for winners, starting at xth place using SELECT

In my MySQL table Winners, I have a list of people who have won.
What I'd like to do is select a list of the names of 10 winners. So what I have right now is this:
SELECT name FROM Winners ORDER BY points DESC LIMIT 10
This returns the first 10 winners which is great.
But how can I make it (for example) return 10 winners, but starting at 20th place? Right now all I can think of is removing the LIMIT and programatically pulling out the 10 winners I want. But I'm sure there's an easier way.
SELECT name
FROM Winners
ORDER BY
points DESC
LIMIT 10 OFFSET 20
or just
SELECT name
FROM Winners
ORDER BY
points DESC
LIMIT 20, 10
SELECT name FROM Winners ORDER BY points DESC LIMIT 20, 10

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