当null时忽略where子句列

问题描述 投票:-1回答:2

我有一个SQL,我希望在其中一个排序子句是NUll时在这样的子句中对它进行排序,然后它应该跳过它并转到下一个排序子句

sql
2个回答
0
投票

使用coalesce,将下一个排序子句放入当前考虑顺序的列中:

SELECT * FROM table
ORDER BY firstcolumn, COALESCE(second_column_might_be_null, thirdcolumn), thirdcolumn

0
投票

订单时的用例

order by case when val is not null then yourdeseired
         else val end asc

或者你可以使用合并

order by COALESCE(col1, col2,999999) //here if both column is null then its order value will be 999999 otherwise it will order according to 1st non null column wise 
© www.soinside.com 2019 - 2024. All rights reserved.