A
A
Artem Volkov2017-02-09 12:50:32
MySQL
Artem Volkov, 2017-02-09 12:50:32

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";

But it does not display as it should (
==============
Here is a screenshot from mysql5f66b3a2312d4edb86ecf285af438abb.PNG

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Fedorov, 2017-02-09
@AronTito

to sort from largest to smallest, use the DESC operator, for example:

SELECT * FROM `article` WHERE `view` > 0  ORDER BY `view` DESC

A
Alexey S., 2017-02-09
@Winsik

add desc at the end, it will sort from largest to smallest
like this then try:
SELECT * FROM `article` ORDER BY `view`+0 DESC

Y
Yevgeniy Kisselyov, 2017-02-09
@ewgenio

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 question

Ask a Question

731 491 924 answers to any question