select * from Article where id in(6,3,4,2,7,1,5)
这条语句输出的记录集排序是按id=1、2、3、4、5、6、7 来排序的,但是我现在要求是要它输出记录时按6,3,4,2,7,1,5的排序输出,咱办啊?
这条语句输出的记录集排序是按id=1、2、3、4、5、6、7 来排序的,但是我现在要求是要它输出记录时按6,3,4,2,7,1,5的排序输出,咱办啊?
解决方案 »
- 问一个分组问题?
- 完全没头绪的SQL操作 高手来哈
- sqlsever2008数据转移到sqlserver 2005上,由于不能使用数据库还原,是不是只能一条一条语句读取插入的方式啊?
- 求救!!!,将数据库中的表失误删除,如何恢复,sql2000,十万火急,谢谢大家了
- 导入数据库之后出现问题 查询出现的问题(从没见过,顶者有分)
- 用存储过程实现复制记录的问题
- 求救!!简单的程序问题,在线等!
- 我没事的时候更改了一下WINDOWS的用户密码,为什么就启动不了SQL Server了?
- 求助弱问题:关于SQL的数据类型
- 疑惑:sql server数据仓库怎么应用在前台asp.net?
- SQL分组查询语句
- 查看角色权限
order by charindex(','+ltrim(id),+',',',6,3,4,2,7,1,5,')
order by (case when id=6 then 1
when id=3 then 2
when id=4 then 3
when id=2 then 4
when id=7 then 5
when id=1 then 6
when id=5 then 7
else 8);
函数 charindex 的参数 3 的数据类型 varchar 无效。
set @a='6,3,4,2,7,1,5'
exec('select * from master..spt_values where number in ( '+ @a +' )
and type=''p''
order by charindex('',''+ltrim(number)+'','','','+@a+','')')
/*
name number type low high status
----------------------------------- ----------- ---- ----------- ----------- -----------
NULL 6 P 1 64 0
NULL 3 P 1 8 0
NULL 4 P 1 16 0
NULL 2 P 1 4 0
NULL 7 P 1 128 0
NULL 1 P 1 2 0
NULL 5 P 1 32 0(7 行受影响)
*/
set @a='6,3,4,2,7,1,5'
print 'select * from master..spt_values where number in ( '+ @a +' )
and type=''p''
order by charindex('',''+ltrim(number)+'','','','+@a+','')'
/*
select * from master..spt_values where number in ( 6,3,4,2,7,1,5 )
and type='p'
order by charindex(','+ltrim(number)+',',',6,3,4,2,7,1,5,')*/
select * from Article where id in (6,3,4,2,7,1,5)
order by charindex(','+ltrim(cast(id as varchar(2))),',',',6,3,4,2,7,1,5,')