I
I
Ivan Ivanov2021-01-24 14:01:53
SQL
Ivan Ivanov, 2021-01-24 14:01:53

How to write given sql query?

You want to display the entries sorted by the position field, however, the entries with the default value of position (which is 0) should be at the end of the list. That is, the list will be like this (field position): 1,2,3,4....0,0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-01-24
@maksim_fix

ORDER BY `position` = 0, `position`
But indexes will not be used for such sorting.

D
d-stream, 2021-01-24
@d-stream

sort not by field, I'm by function value from this field (fields)
for this in sql there is a wonderful case (in addition to ifnull/isnull/nullif etc.)
case when position=0 then 100500 else position end
p/s/ and by the way, it’s worth thinking about whether the default should be null and not 0 - then another aspect of the ordering will be revealed (in a number of dialects) just about null earlier / later than all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question