ORDER BY ASC with Nulls at the Bottom

后端 未结 4 1681
借酒劲吻你
借酒劲吻你 2020-12-03 04:24

I\'m writing an SQL query that connects a schools table to a districts table. Simple One-To-Many relationship where each school is attached to one district. My query is as

4条回答
  •  离开以前
    2020-12-03 05:19

    You could use the ISNULL() function.

    From the MySQL manual:

    ISNULL(expr)

    If expr is NULL, ISNULL() returns 1, otherwise it returns 0.

    For example:

    ORDER BY ISNULL(districts.name), districts.name, schools.name
    

    I like to use this instead of the CASE option for MySQL. Just be aware that it's not portable since ISNULL() is not standard SQL and functions differently in other versions of SQL.

提交回复
热议问题