按一列排序,但在最后放一个特定的内容 [英] order by one column, But put a specific content at the end

查看:39
本文介绍了按一列排序,但在最后放一个特定的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个列.其中两个是:

I have multiple columns. Two of them are:

组(数字,非唯一)名称(字符串,非唯一)

Group (numeric, not unique) Name (string, not unique)

我需要按组降序排序,但是,如果值名称"等于空",我应该显示在组的末尾...

I would need to order by Group descending, BUT, if the value "Name" equals "Empty" i should be displayed at the end of the Group...

Group    Name

1020    test1
1020    test2
1020    test3
1020    EMPTY  <-- end of Group 1020
1020    EMPTY  <-- end of Group 1020
3040    test6
3040    test7
3540    test8
3540    EMPTY  <-- end of Group 3540
15060   test9
15060   EMPTY  <-- end of Group 15060

如何使用 mysql 查询来完成此操作?

How can this be done with a mysql query?

推荐答案

在 MySQL 中,像 Name = 'EMPTY' 这样的表达式的值为 1 if true 和 >0 如果为假.

In MySQL, expressions like Name = 'EMPTY' have the value 1 if true and 0 if false.

所以你可以使用

          ORDER BY Group, Name = 'EMPTY', Name

首先获取所有非空值,然后是所有空值.

to get all the not-EMPTY values first, then all the EMPTY ones.

这篇关于按一列排序,但在最后放一个特定的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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