Create table #t(name varchar(10),age int,state varchar(10)) insert #t select '小明',26,'丙' union all select '小黄',24,'乙' union all select '小青',22,'甲' union all select '小龙',34,'乙' union all select '小张',26,'甲' union all select '小玉',35,'丙'select * from #tselect name,age,state,sort=case state when '甲' then 1 when '乙' then 2 when '丙' then 3 else 4 end from #t order by sort asc drop table #t
如果是 甲甲甲甲乙乙乙乙丙丙丙丙丙这样排序,就这样 SELECT name, age ,state FROM table where state='甲' union all SELECT name, age ,state FROM table where state='乙' union all SELECT name, age ,state FROM table where state='丙'如果是 甲乙丙这样排序,就这样SELECT name, age ,state FROM table order by charindex(state,'甲乙丙')
SELECT name, age ,state FROM table order by charindex(','+ltrim(state)+',',',甲,乙,丙,')
如果是我我只能
select甲的
union all
select已的
union all
select丙的 呵呵
Create table #t(name varchar(10),age int,state varchar(10))
insert #t select '小明',26,'丙' union all
select '小黄',24,'乙' union all
select '小青',22,'甲' union all
select '小龙',34,'乙' union all
select '小张',26,'甲' union all
select '小玉',35,'丙'select * from #tselect name,age,state,sort=case state when '甲' then 1 when '乙' then 2 when '丙' then 3 else 4 end from #t order by sort asc
drop table #t
如果是 甲甲甲甲乙乙乙乙丙丙丙丙丙这样排序,就这样
SELECT name, age ,state FROM table where state='甲'
union all
SELECT name, age ,state FROM table where state='乙'
union all
SELECT name, age ,state FROM table where state='丙'如果是 甲乙丙这样排序,就这样SELECT name, age ,state FROM table order by charindex(state,'甲乙丙')