Sort In Descending Order With NULLs First
Using The IS NOT NULL Comparison Operator:
Similar to the
IS NULL operator, we can rely on the fact that IS NOT NULLreturns 1 when expression is NOT NULL, and 0 otherwise.Query:
SELECT * FROM user ORDER BY date_login IS NOT NULL, date_login DESC
Using
!ISNULL() is equivalent to IS NOT NULL, so either one could be used.
The same query could also be rewritten using
IS NULL like so:SELECT * FROM user ORDER BY date_login IS NULL DESC, date_login DESC
Expected Result:
+----+--------+------------+ | id | name | date_login | +----+--------+------------+ | 2 | john | NULL | | 1 | NULL | 2017-03-12 | | 4 | zayne | 2017-03-02 | | 3 | david | 2016-12-24 | +----+--------+------------+
Không có nhận xét nào:
Đăng nhận xét