34,576
社区成员
发帖
与我相关
我的任务
分享
----测试表名loginlog
select loginlog.* ----这里是你要查询的字段列表 loginlog.* 查询该表所有的字段
from
(select 197 as loginid,1 as id
union
select 188,2
union
select 195,3
----你有多少条件就union上多少条件,但这个临时表的id必需有序,
) as temptable 临时union一个表
,loginlog ----要查询的表名
where temptable.loginid=loginlog.loginid and loginlog.userid='libeibei'----这里加你想要的条件
order by temptable.id
create table abc(id int)
insert abc
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8
go
select *
from abc
order by case when id=1 then 1
when id=2 then 2
when id=4 then 3
when id=8 then 4
when id=7 then 5
when id=3 then 7
when id=6 then 8
when id=5 then 9
end
SELECT id
FROM(
SELECT 1 as id union
SELECT 2 union
SELECT 3 union
SELECT 5
) B
order by charindex(CAST(id as nvarchar(20)),'1,2,5,3')
id
-----------
1
2
5
3
(4 行受影响)
--错了 改下:
select *
from [table]
where charindex(','+ltrim(id)+',',',1,2,4,8,7,3,6,5,')>0
order by charindex(','+ltrim(id)+',',',1,2,4,8,7,3,6,5,')
select *
from table
where
order by case when id=1 then 1
case when id=2 then 2
case when id=4 then 3
case when id=8 then 4
case when id=7 then 5
case when id=3 then 7
case when id=6 then 8
case when id=5 then 9
end