考虑拥有
的数据库checkID record_number data Order_number
a 0 1 2
a 1 2 0
a 2 3 1
a 3 4 3
查找根据Order_number列获取记录的查询,即过程应该返回数据:
having record _number 2 then
having record _number 0 then
having record _number 1 then
having record _number 3
是否有任何联接或其他查询?
答案 0 :(得分:1)
SELECT *
FROM tablename
ORDER BY CASE WHEN record_number = 2 THEN 0 ELSE 1 END, record_number;
这将按以下顺序为您提供数据:
| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
| a | 2 | 3 | 1 |
| a | 0 | 1 | 2 |
| a | 1 | 2 | 0 |
| a | 3 | 4 | 3 |
答案 1 :(得分: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
答案 2 :(得分: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 DEMO 。
| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
| a | 2 | 3 | 1 |
| a | 0 | 1 | 2 |
| a | 1 | 2 | 0 |
| a | 3 | 4 | 3 |
答案 3 :(得分: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
答案 4 :(得分:0)
SELECT *
FROM tablename
ORDER BY Order_number