首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

select * from t1 where pid in(.)排序有关问题

2012-03-13 
select * from t1 where pid in(...)排序问题我的表t1为:ID(主键)pid1122334455667788我的语句:select*fro

select * from t1 where pid in(...)排序问题
我的表   t1   为:

ID(主键)     pid
1                     1
2                     2
3                     3
4                     4
5                     5
6                     6
7                     7
8                     8

我的语句:   select   *   from   t1   where   pid   in(7,6,2,5);

返回的值为:

          2         2
          5         5
          6         6
          7         7

请问如果我想按照   in(7,6,2,5)的顺序返回值,该如何写呢?

预期效果:

        7       7
        6       6
        2       2
        5       5

[解决办法]
select * from t1 where pid in(7,6,2,5)
order by charindex( ', '+rtrim(pid)+ ', ', ',7,6,2,5, ')

热点排行
Bad Request.