[英]sql order by multiple columns with nulls
I have a resultes set that looks like the following in a table var(sorry for the poor formatting): 我在表var中有一个类似于以下内容的结果集(抱歉,格式不正确):
RootID | RootName | ChildId | ChildName
1 | Bob | null | null
1 | null | 4 | Tim
1 | null | 6 | Cindy
2 | Alice | null | null
2 | null | 7 | Joe
2 | null | 9 | Jack
3 | Frank | null | null
3 | null | 17 | ken
What I would like to do is order them by RootName ASC then ChildName ASC to get them in alphabetical order. 我想做的是先按RootName ASC排序,再按ChildName ASC排序,以字母顺序排序。 Any suggestions?
有什么建议么? Thanks in advance
提前致谢
Erm... What about this? 嗯...那呢?
select * from yourtable order by RootName ASC, ChildName ASC
You can use NULLS LAST
and NULLS FIRST
(at least in Oracle) to control, where the nulls should be placed in the sorting. 您可以使用
NULLS LAST
和NULLS FIRST
(至少在Oracle中是这样)来控制将空值放在排序位置的位置。
If it works for your application, I'd rewrite the query to get rid of the nulls. 如果它适用于您的应用程序,我将重写查询以消除空值。 I can't help but think that
我忍不住想
RootID | RootName | ChildId | ChildName
1 | Bob | 4 | Tim
1 | Bob | 6 | Cindy
2 | Alice | null | Joe
2 | Alice | 4 | Jack
3 | Frank | 7 | ken
Would be easier to work with. 会更容易使用。 (Uh, that NULL for Alice/Joe is a typo, right?)
(呃,Alice / Joe的NULL是一个错字,对吧?)
select rec_id,gr_id from jegad order by 1 desc
select isnull(rec_id,'-'),isnull(gr_id,'-') from jegad order by 1 desc
ORDER BY CASE WHEN Col Is NULL Then 1 Else 0 End, Col 当Col为空时按个案排序然后1否则0结束,Col
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.