1

我有一些这样的记录

id  name    sequence
------------------------
1   steve   3
2   lee     2
3   lisa    1
4   john    0
5   smith   0

我想显示如下记录

id  name
------------
1   lisa
2   lee
3   steve
4   john
5   smith

当我使用 order by 子句时,它会显示为

name
----
john
smith
lisa
lee
steve

询问

SELECT name from tbl1 where 1 ORDER BY sequence ASC
4

2 回答 2

5
SELECT name
FROM tbl1 
ORDER BY sequence = 0,
         sequence ASC

或者

SELECT name
FROM tbl1 
ORDER BY case when sequence <> 0 then 1 else 2 end,
         sequence ASC
于 2013-10-30T08:53:14.607 回答
0

ORDER BY您可以在子句中使用带有 if 条件的查询

SELECT
  name
from tbl1
ORDER BY IF(sequence = 0,name,sequence) ASC

小提琴

输出

|  NAME |
|-------|
|  lisa |
|   lee |
| steve |
|  john |
| smith |
于 2013-10-30T08:55:36.490 回答