Declare @t Table (职务 varchar(10))
Insert @t select '总经理'
union all select '经理'
union all select '科长'
union all select '部长'
union all select '员工' Declare @s Varchar(100) set @s = '总经理,部长,员工,科长,经理' select *, CharIndex(',' + 职务 + ',', ',' + @s + ',') from @t --Order by CharIndex(',' + 职务 + ',', ',' + @s + ','--进行排序
select *, CharIndex(',' + 职务 + ',', ',' + @s + ',') from @t Order by CharIndex(',' + 职务 + ',', ',' + @s + ',') ----------------------------------------------------
/*
你可以先考虑这条:CharIndex(',' + 职务 + ',', ',' + @s + ',') ,也就是字段“'职务'”各值在 '总经理,部长,员工,科长,经理' 中的位置,为(1 5 8 11 14 ),然后再按这个位置排序。
当然这个序列就是我们所要求的结果的顺序组合,才能得到想要的结果。
*/
Insert @t select '总经理'
union all select '经理'
union all select '科长'
union all select '部长'
union all select '员工' Declare @s Varchar(100) set @s = '总经理,部长,员工,科长,经理' select *, CharIndex(',' + 职务 + ',', ',' + @s + ',') from @t --Order by CharIndex(',' + 职务 + ',', ',' + @s + ','--进行排序
select *, CharIndex(',' + 职务 + ',', ',' + @s + ',') from @t Order by CharIndex(',' + 职务 + ',', ',' + @s + ',') ----------------------------------------------------
/*
你可以先考虑这条:CharIndex(',' + 职务 + ',', ',' + @s + ',') ,也就是字段“'职务'”各值在 '总经理,部长,员工,科长,经理' 中的位置,为(1 5 8 11 14 ),然后再按这个位置排序。
当然这个序列就是我们所要求的结果的顺序组合,才能得到想要的结果。
*/
Insert @t select '总经理', 's1'
union all select '经理','s2'
union all select '科长', 's3'
union all select '部长', 's4'
union all select '员工', 's5'Declare @s Varchar(100) set @s = '总经理,部长,员工,科长,经理' --select *, CharIndex(',' + 职务 + ',', ',' + @s + ',') from @t --Order by CharIndex(',' + 职务 + ',', ',' + @s + ','select CharIndex(',' + 职务 + ',', ',' + @s + ','), * from @t Order by CharIndex(',' + 职务 + ',', ',' + @s + ',')1:我增加一列, 平常习惯Order By后面跟列名,表示按照这列排序, 现在增加了一列, 请问是对哪一列排序, sql如何判断?
2:对于结果集:
总经理
经理
科长
部长
员工
如何排序成为
总经理
部长
员工
科长
经理
的还是不懂啊