C
C
cheremsha112019-12-02 18:45:17
PHP
cheremsha11, 2019-12-02 18:45:17

How to perform multiple sorting of a table with null values?

Greetings! There is a table where sorting is done by two values

SELECT * FROM mytable 
ORDER BY photo DESC, stats DESC

the status values ​​are integer, but in the case of photo, where either NULL or a link to the photo, the algorithm sorts not by the value is / is not (NULL), but also tries to sort by the link string, knocking down the next sort. That is, the found value of the status is not in descending order, but randomly. How to fix a request?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Decadal, 2018-12-16
@KenKup

The most delicate thing is to first find out if the site whose content you are trying to read works with the parameters you pass.
Also, a space in the address bar must be encoded as %20 (url_encode, read about it).

D
DanKud, 2018-12-16
@DanKud

.....
$str = rawurlencode($_GET['str']);
.....

L
Lazy @BojackHorseman, 2019-12-02
@cheremsha11

ORDER BY photo IS NOT NULL DESC, stats DESC

D
d-stream, 2019-12-03
@d-stream

the most classic and for more advanced logic - order by case when stats is null then <largest or smallest value here> else stats end desc
well, or how to set it at the NULLS level in a number of sql dialects { FIRST | LAST}
or all sorts of hacks like order by -stats asc or just wrap it in isnull

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question