SQL排序问题。。。

ppsniffer 2011-03-13 09:21:57

SELECT TOP 10 * FROM [book] where bookid in (1065003,1067113,1039748,1067630,1066993,1061085,1060138,1058105,1063632,1056890,1064456,1066585,1062870,1055856)


查询结果怎样根据()括号里面的顺序排序?

比如ID为1065003排第一,1067113第二这样。。
...全文
84 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yishengjihe 2011-03-14
  • 打赏
  • 举报
回复
无水先生 2011-03-14
  • 打赏
  • 举报
回复
SELECT TOP 10 * FROM [book] where bookid in (1065003,1067113,1039748,1067630,1066993,1061085,1060138,1058105,1063632,1056890,1064456,1066585,1062870,1055856) ording by bookid;
快溜 2011-03-13
  • 打赏
  • 举报
回复
何必呢。。
Shawn 2011-03-13
  • 打赏
  • 举报
回复
create table #BOOK
(
bookid int,
[name] varchar(10)
)
insert #BOOK(bookid)
select 1066993 union all
select 1061085 union all
select 1067113 union all
select 1039748 union all
select 1064456 union all
select 1066585 union all
select 1060138 union all
select 1058105 union all
select 1063632 union all
select 1067630 union all
select 1056890 union all
select 1066993 union all
select 1055856
go
--SQL:
;with cte as
(
select rowno = ROW_NUMBER() over(order by getdate()), * from
(
select bookid = 1065003 union all
select 1067113 union all
select 1039748 union all
select 1067630 union all
select 1066993 union all
select 1061085 union all
select 1060138 union all
select 1058105 union all
select 1063632 union all
select 1056890 union all
select 1064456 union all
select 1066585 union all
select 1062870 union all
select 1055856
) t
)
select a.* from
(SELECT TOP 10 * FROM #BOOK where bookid in (select bookid from cte)) a
inner join cte b
on a.bookid = b.bookid
order by b.rowno
haitao 2011-03-13
  • 打赏
  • 举报
回复
直接加上order by bookid不行吗?
rucypli 2011-03-13
  • 打赏
  • 举报
回复
直接order by bookid
ljking0731 2011-03-13
  • 打赏
  • 举报
回复
SELECT TOP 10 * FROM [book] where bookid in (1065003,1067113,1039748,1067630,1066993,1061085,1060138,1058105,1063632,1056890,1064456,1066585,1062870,1055856) 
order by charindex(','+cast(bookid as varchar)+',',',1065003,1067113,1039748,1067630,1066993,1061085,1060138,1058105,1063632,1056890,1064456,1066585,1062870,1055856,')



ljking0731 2011-03-13
  • 打赏
  • 举报
回复

SELECT TOP 10 * FROM [book] where bookid in (1065003,1067113,1039748,1067630,1066993,1061085,1060138,1058105,1063632,1056890,1064456,1066585,1062870,1055856)
order by charindex(','+bookid+',',',1065003,1067113,1039748,1067630,1066993,1061085,1060138,1058105,1063632,1056890,1064456,1066585,1062870,1055856,')



34,594

社区成员

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

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