我这个sql语句如何让它输出排序?

fhquemc 2010-07-03 09:54:30
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的排序输出,咱办啊?
...全文
169 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fhquemc 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sql_hhy 的回复:]
SQL code
declare @a varchar(20)
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+','……
[/Quote]

谢谢你!现在可以了
otnto 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sql_hhy 的回复:]

SQL code
select * from Article where id in(6,3,4,2,7,1,5)
order by charindex(','+ltrim(id),+',',',6,3,4,2,7,1,5,')
[/Quote]
UP,妙
claro 2010-07-03
  • 打赏
  • 举报
回复
--try
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,')
宇峰科技 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sql_hhy 的回复:]
SQL code
select * from Article where id in(6,3,4,2,7,1,5)
order by charindex(','+ltrim(id),+',',',6,3,4,2,7,1,5,')
[/Quote]学习
SQL_Hhy 2010-07-03
  • 打赏
  • 举报
回复
declare @a varchar(20)
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,')

*/
SQL_Hhy 2010-07-03
  • 打赏
  • 举报
回复
declare @a varchar(20)
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 行受影响)
*/
fhquemc 2010-07-03
  • 打赏
  • 举报
回复
要报错啊
函数 charindex 的参数 3 的数据类型 varchar 无效。
Mr_Nice 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sql_hhy 的回复:]

SQL code
select * from Article where id in(6,3,4,2,7,1,5)
order by charindex(','+ltrim(id),+',',',6,3,4,2,7,1,5,')
[/Quote]

up
SQL77 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sql_hhy 的回复:]
SQL code
select * from Article where id in(6,3,4,2,7,1,5)
order by charindex(','+ltrim(id),+',',',6,3,4,2,7,1,5,')
[/Quote]
UP..
luoyoumou 2010-07-03
  • 打赏
  • 举报
回复
select * from Article where id in(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);
SQL_Hhy 2010-07-03
  • 打赏
  • 举报
回复
select * from Article where id in(6,3,4,2,7,1,5)
order by charindex(','+ltrim(id),+',',',6,3,4,2,7,1,5,')

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧