Order by NULL values with mysql

megadave

I have a table like this:

id            description status   login
XWPggD        bbbbbbbb    1        js
0JIERf        test1       1        js
0gd2x0        nothing     NULL     js
bSIUIu        dev         NULL     bob
0BNh27        hello       1        js
2TYXjd        down        NULL     inge
axE1m5        bobby       NULL     bob
1iSlQM        qwe         0        js
9dPjoP        descr       NULL     inge

I'm trying to sort the output by status(null values at the bottom) and then by login. But I also want the login values, where the status is NULL to be ordered correctly, so that it looks like:

id            description status   login
XWPggD        bbbbbbbb    1        js
0JIERf        test1       1        js
0BNh27        hello       1        js
1iSlQM        qwe         0        js
axE1m5        bobby       NULL     bob
bSIUIu        dev         NULL     bob
9dPjoP        descr       NULL     inge
2TYXjd        down        NULL     inge
0gd2x0        nothing     NULL     js

But when I try something like:

SELECT id, description, status, login FROM dev
ORDER BY isnull(status) ASC, isnull(login) ASC;

I always get a weird loginorder where status values are NULL.

steffen

Why so complicated answers ...

SELECT id, description, status, login FROM dev
ORDER BY status IS NULL, status DESC, login IS NULL, login DESC

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql order by field with NULL values last

From Dev

MySQL Update null column values incrementally in time order

From Dev

Is it safe to rely on the order of null values in an order by?

From Dev

Is it safe to rely on the order of null values in an order by?

From Dev

Mysql TIMEDIFF NULL values

From Dev

MySQL cardinality of NULL values

From Dev

MYSQL - COUNT() NULL Values

From Dev

MySQL Order by Date, with NULL first

From Dev

MySQL Order by Date, with NULL first

From Dev

mysql custom order excluding Values

From Dev

How to order by various values in MySQL?

From Dev

MySQL closest values order by count

From Dev

mysql 'ORDER BY' joined columns with NO Values

From Dev

Mysql Order by mix values then other values

From Dev

Mysql Order by mix values then other values

From Dev

Oracle - Order by column with NULL values in last position

From Dev

AngularJS and null select values dependent on selection order

From Dev

LINQ Order By Descending with Null Values on Bottom

From Dev

Why does this SQL order null values last?

From Dev

Doctrine 2 Order By ASC and Null values in last

From Dev

Oracle - Order by column with NULL values in last position

From Dev

Incorrect sorting order of NULL values with a SQL view

From Dev

Why does this SQL order null values last?

From Dev

MySQL left join with NULL values

From Dev

MySQL where clause with null values

From Dev

MYSQL Null and empty values sorting

From Dev

Get NULL values in a MySQL query

From Dev

MySQL Insert error with null values

From Dev

Results not showing in MySQL with NULL values