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;
Related
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
I want to display trending Ads on my site on the basis of number of clicks.
My SQL Query looks like this:
SELECT ad_id, clicks from ads ORDER BY clicks DESC LIMIT 5
Result:
ad_id clicks
3393 2204
4495 1208
2399 932
2780 777
3316 679
I want to display this result randomly every time page refresh.
I tried using
SELECT * from ads ORDER BY clicks DESC, RAND() LIMIT 10
But this is not working giving the same result every time on page refresh.
If I use like:
SELECT ad_id, clicks from ads ORDER BY RAND(), clicks DESC LIMIT 10
ad_id clicks
9762 0
6305 1
4040 17
11598 0
11347 0
It is showing data randomly but now the highest number of clicks is zero.
Can you suggest me how to display top clicks result randomly on every page refresh.
If you want to get top 10 rows and then display them in random order, use this:
select *
from (
select *
from ads
order by clicks desc LIMIT 10
) t
order by rand() desc
If you want to get 10 randomly chosen records sorted in descending order of clicks, try this:
select *
from (
select *
from ads
order by RAND() LIMIT 10
) t
order by clicks desc
It finds 10 random records in subquery and then sorts it afterwards.
Or perhaps you want to get 10 random records out of some top , say 100, rows sorted in descending order of clicks:
select *
from (
select *
from (
select *
from ads
order by click desc LIMIT 100 -- change this as per your needs
) t
order by rand() limit 10
) t
order by clicks desc
I am using this query but it is not working in mysql
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM ads ORDER BY id DESC) ads ORDER BY id DESC
You can use ORDER BY ... LIMIT {[offset,] row_count | row_count OFFSET offset} (lets say you want to get 5 records from 10th - 10,11,12,13,14):
SELECT * FROM ads
ORDER BY id DESC
LIMIT 10,5
Although I assume you don't want to get them sorted by id but rather by views or similar criteria where ORDER BY views DESC would take a place (don't forget to to add index on views count).
The 'TOP' does not function on MySQL. You can edit the query in the following manner to get the job done
SELECT * FROM (SELECT * FROM ads ORDER BY id DESC LIMIT 5) ads ORDER BY id DESC LIMIT 5
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.
I'm using full text search to pull rows.
I order the rows based on score (ORDER BY SCORE) , then of the top 20 rows (LIMIT 20), I want to rand (RAND) the result set.
So for any specific search term, I want to randomly show 5 of the top 20 results.
My workaround is code based- where I put the top 20 into an array then randomly select 5.
Is there sql way to do this?
You can do this using an inner select. Select the top twenty rows in the inner select. In the outer select order these rows randomly and select the top five:
SELECT *
FROM (
SELECT *
FROM table1
ORDER BY score DESC
LIMIT 20
) AS T1
ORDER BY RAND()
LIMIT 5