Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query for top 100, displayed in reverse

Tags:

sql

mysql

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.

like image 545
blogo Avatar asked May 13 '17 02:05

blogo


People also ask

What is the opposite of top in SQL query?

You can reverse the ordering by using DESC instead of ASC at the end of your query.

How do you reverse a view in SQL?

SQL Server REVERSE() Function The REVERSE() function reverses a string and returns the result.

How do I get last 10 rows in SQL Server?

Let us now implement the above query. mysql> SELECT * FROM ( -> SELECT * FROM Last10RecordsDemo ORDER BY id DESC LIMIT 10 -> )Var1 -> -> ORDER BY id ASC; The following is the output that displays the last 10 records. We can match both records with the help of the SELECT statement.


1 Answers

Use a subquery:

SELECT t.*
FROM (SELECT *
      FROM tbllogos
      WHERE status = 'live'
      ORDER BY score DESC
      LIMIT 100
     ) t
ORDER BY score ASC;
like image 104
Gordon Linoff Avatar answered Sep 29 '22 06:09

Gordon Linoff