Answer the question
In order to leave comments, you need to log in
How to build a mysql query with ordered output?
Hello.
Here is such a funny problem at first glance. There is a table in mysql articles "article" and it has a column view, where there are numbers. These numbers show the number of views of the article
id: 1
view: 100
id: 2
view: 300
id: 3
view: 500
id: 4
view: 50
etc.
I need to build a simple query to display them from the database by the number of views from the largest to the smallest.
I wrote like this:
$sql = "SELECT * FROM `article` WHERE view > 0 ORDER BY view";
Answer the question
In order to leave comments, you need to log in
to sort from largest to smallest, use the DESC operator, for example:
SELECT * FROM `article` WHERE `view` > 0 ORDER BY `view` DESC
add desc at the end, it will sort from largest to smallest
like this then try:SELECT * FROM `article` ORDER BY `view`+0 DESC
If field type is not int try ABS
SELECT * FROM `article` WHERE `view` > 0 ORDER BY ABS(`view`) DESC
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question