Postgresql 按第一列排序,但第二列最后有空值或零
Postgresql order by first column, but with nulls or zeros in second column last
如何对具有多个列的 table 进行排序,其中按第一列的字母顺序排序,但第二列中所有具有空值或零的字母条目出现在最后。
排序前
Table"user"
username monies zip
jim 25 87888
allan 12 34333
adrian 0 97677
abel null 87888
will 4 88788
排序后
Table"user"
username monies zip
allan 12 34333
jim 25 87888
will 4 88788
abel null 87888
adrian 0 97677
我试过了,但没用
SELECT
*
FROM
"user"
ORDER BY
name ASC,
monies nulls last
只需先对 0
或 null
进行排序,然后按用户名
进行字母排序
order by case when monies = 0 or monies is null then 1 else 0 end, username
如何对具有多个列的 table 进行排序,其中按第一列的字母顺序排序,但第二列中所有具有空值或零的字母条目出现在最后。
排序前 Table"user"
username monies zip
jim 25 87888
allan 12 34333
adrian 0 97677
abel null 87888
will 4 88788
排序后 Table"user"
username monies zip
allan 12 34333
jim 25 87888
will 4 88788
abel null 87888
adrian 0 97677
我试过了,但没用
SELECT
*
FROM
"user"
ORDER BY
name ASC,
monies nulls last
只需先对 0
或 null
进行排序,然后按用户名
order by case when monies = 0 or monies is null then 1 else 0 end, username