MySQL order by a number, nulls last

Obecnie wykonuję bardzo podstawowy rozkaz w moim oświadczeniu.

SELECT * FROM tablename WHERE visible=1 ORDER BY position ASC, id DESC

Problem polega na tym, że wpisy NULL dla 'position' są traktowane jako 0. Dlatego wszystkie wpisy z pozycją jako NULL pojawiają się przed tymi z 1,2,3,4. eg:

NULL, NULL, NULL, 1, 2, 3, 4

Czy istnieje sposób na osiągnięcie następującej kolejności:

1, 2, 3, 4, NULL, NULL, NULL.
Author: hakre, 2010-01-12

10 answers

MySQL ma nieudokumentowaną składnię do sortowania null jako ostatnia. Umieść znak minus ( - ) przed nazwą kolumny i przełącz ASC na DESC:

SELECT * FROM tablename WHERE visible=1 ORDER BY -position DESC, id DESC

Jest to zasadniczo odwrotność position DESC umieszczania wartości NULL na końcu, ale poza tym taka sama jak position ASC.

Dobre odniesienie jest tutaj http://troels.arvin.dk/db/rdbms#select-order_by

 469
Author: user1052645,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-17 20:43:19

Uznałem to za dobre rozwiązanie w większości przypadków:

SELECT * FROM table ORDER BY ISNULL(field), field ASC;
 266
Author: d-_-b,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-16 14:42:17

Coś jak

SELECT * FROM tablename where visible=1 ORDER BY COALESCE(position, 999999999) ASC, id DESC

Zamień 9999999999 na wartość maksymalną pola

 25
Author: DrewM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-12 19:13:32

Spróbuj użyć tego zapytania:

SELECT * FROM tablename
WHERE visible=1 
ORDER BY 
CASE WHEN position IS NULL THEN 1 ELSE 0 END ASC,id DESC
 4
Author: Rachit Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-02 15:49:54

Możesz połączyć swoje nulle w ORDER BY stwierdzeniu:

select * from tablename
where <conditions>
order by
    coalesce(position, 0) ASC, 
    id DESC

Jeśli chcesz, aby nulle posortowały się na dole, spróbuj coalesce(position, 100000). (Aby druga liczba była większa niż wszystkie pozostałe position ' s W db.)

 3
Author: Seth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-12 19:14:42
SELECT * FROM tablename WHERE visible=1 ORDER BY CASE WHEN `position` = 0 THEN 'a' END , position ASC
 3
Author: YasirPoongadan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-19 11:29:32

Można zamienić instancje NULL z inną wartością, aby posortować je jako pierwsze (np. 0 lub -1) lub jako ostatnie (duża liczba lub litera)...

SELECT field1, IF(field2 IS NULL, 9999, field2) as ordered_field2
  FROM tablename
 WHERE visible = 1
 ORDER BY ordered_field2 ASC, id DESC
 2
Author: Langdon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-12 19:23:17

Do kolumny DATE możesz użyć:


NULLS last:

ORDER BY IFNULL(`myDate`, '9999-12-31') ASC

Blanks last:

ORDER BY IF(`myDate` = '', '9999-12-31', `myDate`) ASC
 2
Author: Danny Beckett,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-15 22:33:19

NULL LAST

SELECT * FROM table_name ORDER BY id IS NULL, id ASC
 2
Author: reverbnation,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-20 08:52:37

Dlaczego nie zamówisz przez NULLS jako ostatni?

SELECT * 
FROM tablename
WHERE visible = 1 
ORDER BY position ASC NULLS LAST, id DESC 
 -6
Author: RedRover,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-13 15:05:22