select * from aaa where uid in(1,3,2) order by uid
select * from aaa where uid in(1,3,2) order by case when uid=1 then 1 when uid=3 then 2 when uid=2 then 3 end
select * from aaa where uid = 1 union all select * from aaa where uid = 3 union all select * from aaa where uid = 2
select * from aaa where uid in(1,3,2) order by case when uid=1 then 1 else 2 end,uid desc找规律
Create table aaa(Id int identity,Name varchar(10)) insert into aaa select 'a' insert into aaa select 'b' insert into aaa select 'c' insert into aaa select 'd' insert into aaa select 'e' insert into aaa select 'F'----创建表变量# select top 1000 Id=identity(int,1,1) into # from sysobjects a,sysobjects b declare @Uid varchar(20) set @Uid='1,3,2,5,4' select a.* from aaa a,( select name=substring(a.name,b.id,charindex(',',name+',',b.id)-b.id),b.id from (select @uid as name) a,# b where substring(','+name,b.id,1)=',' ) b where a.id=b.name order by b.id /* Id Name ----------- ---------- 1 a 3 c 2 b 5 e 4 d(5 行受影响) */
select * from aaa where uid in(1,3,2)
order by case when uid=1 then 1
when uid=3 then 2
when uid=2 then 3
end
select * from aaa where uid = 1
union all
select * from aaa where uid = 3
union all
select * from aaa where uid = 2
Create table aaa(Id int identity,Name varchar(10))
insert into aaa select 'a'
insert into aaa select 'b'
insert into aaa select 'c'
insert into aaa select 'd'
insert into aaa select 'e'
insert into aaa select 'F'----创建表变量#
select top 1000 Id=identity(int,1,1) into # from sysobjects a,sysobjects b
declare @Uid varchar(20)
set @Uid='1,3,2,5,4'
select a.* from aaa a,(
select name=substring(a.name,b.id,charindex(',',name+',',b.id)-b.id),b.id from (select @uid as name) a,# b
where substring(','+name,b.id,1)=','
) b
where a.id=b.name
order by b.id
/*
Id Name
----------- ----------
1 a
3 c
2 b
5 e
4 d(5 行受影响)
*/