如何在sql中使用like,或者和orderby一起使用

时间:2016-12-12 09:15:38

标签: sql sql-server

需要选择'codevalue',基于 like pattern ,然后订购选择指定订单 。我已经做了以下查询,但sql通过错误消息msg

  

关键字'WHEN'附近的语法不正确。

我在哪里犯了错误?

select * from basevalue 
where codevalue like '01%' 
or codevalue like '02%' 
or codevalue like '03%' 
or codevalue like '04%' 
or codevalue like '05%' 
or codevalue like '00%' 
order by 
     WHEN codevalue like '03%' THEN 1
     WHEN codevalue like '04%' THEN 2
     WHEN codevalue like '05%' THEN 3
     WHEN codevalue like '00%' THEN 4
     WHEN codevalue like '01%' THEN 5
     WHEN codevalue like '02%' THEN 6
     END

1 个答案:

答案 0 :(得分:7)

它应该是这样的,你忘了CASEELSE

select * from basevalue 
where codevalue like '01%' 
or codevalue like '02%' 
or codevalue like '03%' 
or codevalue like '04%' 
or codevalue like '05%' 
or codevalue like '00%' 
order by CASE
     WHEN codevalue like '03%' THEN 1
     WHEN codevalue like '04%' THEN 2
     WHEN codevalue like '05%' THEN 3
     WHEN codevalue like '00%' THEN 4
     WHEN codevalue like '01%' THEN 5
     ELSE 6
     END`