在做项目页面显示中,有时需要按照指定字符串顺序排序,根据检索条件把检索结果排序。SqlServer 的charindex 语法可以实现对字段按照指定的顺序排序。
order by 中包含的条件在其他数据之前
select * from 表名
where 列名 in ('条件1','条件2','条件3')
order by charindex(列名,'条件1条件2') -- 注意: 条件1条件2 中间没有隔开
例如: 1.表Test 字段栏位 :status,id , type..... 查询type='lu'的数据, 首先按照状态Y,W,N,F 排序,然后根据id 降序排列
select id,status from Test where type='lu' order by charindex(status, 'YWNF'),id desc
2.表Test 字段栏位 :status,id , type..... 查询type='lu'的数据, 首先按照状态Y,W,N,F 排序,然后根据id 升序排列
select id,status from Test where type='lu' order by charindex(status, 'YWNF'),id aes
|