如何通过sql Query按顺序放置条件

时间:2014-06-05 09:48:30

标签: mysql sql

这是我的表名: - xyztable,在这张表中我有3列

A: - id
B: - 姓名
c: - Order_number

我有像

这样的样本数据
Id Name order_nmber  
1  A    1  
2  K    0  
3  B    6  
4  c    3  
5  P    0  
6  d    5  
7  e    2  
8  O    0  

我写的查询是

select  * from xyztable order by order_number ASC;

给我的结果是

Id Name order_nmber 
2  K    0
5  P    0
8  O    0
1  A    1
7  e    2
4  c    3 
6  d    5
3  B    6 

但我希望结果如下: -

Id Name order_nmber 
1  A    1
7  e    2
4  c    3 
6  d    5
3  B    6
2  K    0
5  P    0
8  O    0

那我怎么能在单个查询中得到这个....请帮助!!!!

3 个答案:

答案 0 :(得分:7)

你可以这样做:

select  * from xyztable order by order_number = 0, order_number ASC;

答案 1 :(得分:1)

select  * from xyztable 
order by  case when order_number=0 then 1 else 0 end ASC, order_number ASC;

答案 2 :(得分:1)

尝试这样的事情:

SELECT * FROM xyztable ORDER BY order_number = 0, order_number;