3

考虑一个数据库

checkID record_number   data    Order_number
a                0        1        2
a                1        2        0
a                2        3        1
a                3        4        3

查找根据 Order_number 列获取记录的查询 ieprocedure 应该返回数据:

having record _number 2 then
having record _number 0 then
having record _number 1 then
having record _number 3 

是否有任何连接或其他查询?

4

5 回答 5

1
SELECT *
FROM tablename
ORDER BY CASE WHEN record_number = 2 THEN 0 ELSE 1 END, record_number;

SQL 小提琴演示

这将按以下顺序为您提供数据:

| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
|       a |             2 |    3 |            1 |
|       a |             0 |    1 |            2 |
|       a |             1 |    2 |            0 |
|       a |             3 |    4 |            3 |
于 2013-02-20T10:06:42.077 回答
1
select  *
from    YourTable
order by
        case record_number
        when 2 then 1
        when 0 then 2
        when 1 then 3
        when 3 then 4
        end
于 2013-02-20T10:06:56.363 回答
1

在这种情况下,您可以使用CASE WHEN 。

SELECT  *
FROM    tbl
ORDER BY CASE record_number
              WHEN 2 THEN 1
              WHEN 0 THEN 2
              WHEN 1 THEN 3
              WHEN 3 THEN 4
              END

这是SQL Fiddle 演示

| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
|       a |             2 |    3 |            1 |
|       a |             0 |    1 |            2 |
|       a |             1 |    2 |            0 |
|       a |             3 |    4 |            3 |
于 2013-02-20T10:07:54.563 回答
1

最好的解决方案是为排序检查创建额外的表,如下所示:

record_number   sort_seq
     2              1
     0              2
     1              3
     3              4

然后把它和你的桌子一起加入

select c.* 
from check as c 
     join sortSeq as s on c.record_number=s.record_number 
order by s.sort_seq
于 2013-02-20T10:11:50.797 回答
0
SELECT *
FROM tablename
ORDER BY Order_number 
于 2013-02-20T10:41:33.907 回答