怎么按照指定字段的值序列排序

tuoluofo 2009-06-09 09:53:35


1
2
3
4
5
6
7
8

select *
from table
where

order by id(1,2,4,8,7,3,6,5) 这么写肯定不对,我就是这个意思

结果是
1
2
4
8
7
3
6
5

...全文
178 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
流浪河 2009-06-11
  • 打赏
  • 举报
回复
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
WHERE id IN(4,1,3,7,8)
ORDER BY CHARINDEX(','+Convert(varchar(10),id)+',',',4,1,3,7,8,')
rightyeah 2009-06-11
  • 打赏
  • 举报
回复
3楼的办法好牛啊
「已注销」 2009-06-11
  • 打赏
  • 举报
回复

----测试表名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

以前回答的答案

参见http://topic.csdn.net/u/20090423/14/ac350b38-2739-40b5-a737-e3a704bcd765.html
ITniao 2009-06-11
  • 打赏
  • 举报
回复
建议: 数据 多了还是新增个 顺序表 来关联.
dydy39936 2009-06-10
  • 打赏
  • 举报
回复
没看懂!
笑的自然 2009-06-10
  • 打赏
  • 举报
回复
问题很有哲理性!支持!
越过越咸 2009-06-10
  • 打赏
  • 举报
回复
192.168.1.100:回帖是一种美德!每天回帖即可获得 10 分可用分
needanothercoder 2009-06-09
  • 打赏
  • 举报
回复

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

结果如下:
id
1
2
4
8
7
3
6
5
needanothercoder 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 tuoluofo 的回复:]
明白了。

但是charindex可能容易出现问题吧,比如 1,222,11,111,都含有1。

所以还是join 一个排序子查询稳妥。
[/Quote]
是的,我也这么认为
tuoluofo 2009-06-09
  • 打赏
  • 举报
回复
明白了。

但是charindex可能容易出现问题吧,比如 1,222,11,111,都含有1。

所以还是join 一个排序子查询稳妥。
--小F-- 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 tuoluofo 的回复:]
自己也想明白了


select a.pn
from a
right join
(
select 5 pn
union all
select 1 pn
)b
on a.pn=b.pn

没看懂

order by charindex(CAST(id as nvarchar(20)),'1,2,5,3') 是什么意思?

CAST(id as nvarchar(20))转换ID为nvarchar
charindex的用法:
CHARINDEX
返回字符串中指定表达式的起始位置。

语法
CHARINDEX ( expression1 , expression2 [ , start_location ] )

参数
expression1

一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。

expression2

一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。

start_location

在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索


[/Quote]
tuoluofo 2009-06-09
  • 打赏
  • 举报
回复
自己也想明白了


select a.pn
from a
right join
(
select 5 pn
union all
select 1 pn
)b
on a.pn=b.pn

但没看懂

order by charindex(CAST(id as nvarchar(20)),'1,2,5,3') 是什么意思?
--小F-- 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wufeng4552 的回复:]
引用 3 楼 happyflystone 的回复:
比如这个列为id


SQL codeselect *
from table
where
order by charindex(','+ltrim(id)+',',',1,2,4,8,7,3,6,5,')


...
[/Quote]

石头哥的差条件 where后面的条件呢??
tuoluofo 2009-06-09
  • 打赏
  • 举报
回复
自己也想明白了


select a.pn
from a
right join
(
select 5 pn
union all
select 1 pn
)b
on a.pn=b.pn

没看懂

order by charindex(CAST(id as nvarchar(20)),'1,2,5,3') 是什么意思?
水族杰纶 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happyflystone 的回复:]
比如这个列为id



SQL codeselect *
from table
where
order by charindex(','+ltrim(id)+',',',1,2,4,8,7,3,6,5,')
[/Quote]
...
ai_li7758521 2009-06-09
  • 打赏
  • 举报
回复
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 行受影响)
--小F-- 2009-06-09
  • 打赏
  • 举报
回复
--错了 改下:
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,')
Zoezs 2009-06-09
  • 打赏
  • 举报
回复

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
needanothercoder 2009-06-09
  • 打赏
  • 举报
回复
你得建立一个表t,专用来存放这个顺序,如下

a v
1 1
2 2
3 4
4 8
5 7
6 3
7 6
8 5
select a.* from 你要排的表 a join t b on a.v=b.v order by b.a
加载更多回复(6)

34,576

社区成员

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

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