SQL:如何在 orderby 中将空条目和空条目排序到前面? [英] SQL: How can I order null and empty entries to the front in an orderby?

查看:37
本文介绍了SQL:如何在 orderby 中将空条目和空条目排序到前面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的数据库中有以下条目:

If I have the following entries in my database:

身份证名称
1 [空]
2 [空字符串]
3 阿尔法
4 加油
5查理

ID Name
1 [null]
2 [empty string]
3 Alpha
4 Bravo
5 Charlie

..那么如何在使用 ORDER BY 时将名称排在最前面的行排序?

..then how can I order the rows with names to the front when using ORDER BY?

如果我使用 ORDER BY Name,我会得到上面的列表,但我实际上想要:

If I use ORDER BY Name, I get the list above, but I actually want:

3 阿尔法
4 加油
5 查理
1 [空]
2 ''

3 Alpha
4 Bravo
5 Charlie
1 [null]
2 ''

推荐答案

ORDER BY 
    CASE 
    WHEN Name IS NULL THEN 1 
    WHEN Name = ''    THEN 2 
    ELSE 3 
    END DESC, 
    Name ASC

这篇关于SQL:如何在 orderby 中将空条目和空条目排序到前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆