Query MSQL for winners, starting at xth place using SELECT - mysql

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

Related

MySQL Select the highest from last 50 rows

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

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;

MySQL - Getting Top n records and ordering by a specific column with pagination

I have got a problem with getting top n records from database and ordering them by a specific column and paginating them.
For example I want first 100 movies from movie table and order these first 100 records by name and display 10 records per page.
However this doesn't work;
SELECT name FROM movies ORDER BY id DESC, name DESC, LIMIT 0,10
I am quite confused here. In order to paginate I have to use LIMIT in such ways;
LIMIT 0,10 = FIRST PAGE
LIMIT 10,20 = SECOND PAGE
and so on.
In order to get first records, I use ORDER BY id DESC however when I want to list FROM Z to A, ORDER BY id DESC, name DESC doesn't do the trick.
In another words what I want to do is to get first (latest) 100 records out of 10.000 and order this 100 records by name (ASC or DESC) and / or by view (ASC or DESC).
I hope I was clear enough to explain my problem.
I will be glad if you could help me out with this one.
Shift the order of your order by statements
(updated)
select name
from ( select *
from movies
order by id desc
limit 100 )
order by name desc
limit 0,10
It uses the first one first and if equal it looks at the next one
You could try to use
select name from
(select name from movies order by id desc limit 0,100)
order by name desc limit 0,10
Wrap your select in another select;
SELECT name FROM (SELECT * FROM movies ORDER BY id DESC LIMIT 0,100) ORDER BY name DESC LIMIT 0,10
Edit: Updated limits.

Select specific rows MySQL

How can I select specific rows in a MySQL Table? So instead of selecting ... ORDER BY date can I do something like SELECT * FROM Posts FROM(5) TO(10) ORDER BY date DESC where I would be selecting Posts 5 to 10 in the descending order?
Your question is a little ambiguous. There are two possibilities:
You want only part of the results to be retrieved from the database, for example from the 5th one to the 10th one:
SELECT * FROM `posts` ORDER BY `date` DESC LIMIT 6 OFFSET 4
which will skip the 4 first results and given you next 6 results (beginning with 5th from the original set and ending with 10th from the original set).
You want results with specific IDs between 5 (inclusive) and 10 (inclusive):
SELECT * FROM `posts` WHERE `id` BETWEEN 5 AND 10 ORDER BY `date` DESC
or (which can be useful if the list of IDs is not the whole range):
SELECT * FROM `posts` WHERE `id` IN (5,6,7,8,9,10) ORDER BY `date` DESC
but this last example is only to show you different way of picking "specific" rows.
use limit:
SELECT * FROM Posts ORDER BY date DESC LIMIT 5, 5
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
You can use limit operator in mysql to do this. Ex: SELECT * FROM posts LIMIT 5 , 5;
Alternative Syntax which is more readable in my opinion:
SELECT * FROM Posts ORDER BY date DESC LIMIT 5 OFFSET 5
The first number indicates the number of results returned, the second one defines the offset from where to begin with.
If you use the LIMIT 5,5 it's the other way round.
You can use the limit:
SELECT * FROM tabel ORDER BY date DESC LIMIT 5,5
(5th till 10th record based on descending date).
SELECT * FROM Posts ORDER BY date DESC limit 5, 10

to get top 5 rankings from database

In my table i have team and points column and I want to get top 5 teams .Teams with same points should be grouped and consider as one of the ranks so if 5 teams are having same points then all should come as one of the rank and next suceeding records according to team points
TRY
SELECT DISTINCT(point), team
FROM tableTeam
ORDER BY points DESC LIMIT 5
SELECT team,
points,
(SELECT COUNT(*)
FROM teams t2
WHERE t2.points > t1.points) + 1 rank
FROM teams t1
ORDER BY points DESC
LIMIT 5
There's no window functions in MySQL, so you'll want to extract the rank in your scripts.
Also, if I'm making sense of your ranking criteria, you're actually interested in getting the top 5 teams plus any additional teams that might have the same number of points as that in the 5th rank.
If so, your limit should be applied to a subquery on the point criteria:
select name, points
from teams
where points >= (
select points
from teams
order by points desc
limit 1 offset 4
)
order by points desc, name
If not, a simple order by/limit will do:
select name, points
from teams
order by points desc, name
limit 5