See table structure

1
2
3
4
5
6
7
8
9
10
11
12
desc t1;
+----+-----+
| id | num |
+----+-----+
| 1 | -1 |
| 2 | 0 |
| 3 | 1 |
| 4 | 2 |
| 5 | 3 |
| 6 | 4 |
| 7 | 5 |
+----+-----+

How to get a result by select which is num=3 at the first of list? #1

select * from t1 order by case when num = 3 then 1 else 0 end desc, num asc;

Making suborder, it will make a subfield invisible when num=3 and it’s value is 1 and others is 0. #2

1
select * from users order by field(num,3) desc,num asc;

Same way to do it again. Than we can get select result like this:

1
2
3
4
5
6
7
8
id	num
5 3
1 -1
2 0
3 1
4 2
6 4
7 5

Those are super genius ways to pick some row out for some reason. but #1 is more easier to comprehend. 今天干活的时候遇到了一个非常无语的问题,产品列表中因为业务问题又又又又又又要调整顺序。 谁让他是老板呢 就是这么随性。 这次是一个很少用到的特别的排序问题。需要把某个Column等于特殊值的rows放在前面。 几经搜索后发现了上面的方法。其实就是分条件搜索都一种应用,case then 更便于理解. EOF.