Hiển thị các bài đăng có nhãn Null First. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Null First. Hiển thị tất cả bài đăng

Mysql: Sort In Descending Order With NULLs First

Source: https://www.designcise.com/web/tutorial/how-to-order-null-values-first-or-last-in-mysql

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 |
+----+--------+------------+

Renewing Facebook Graph API token automatically?

  Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...