注册 登录
编程论坛 SQL Server论坛

[求助]查询问题

CrazyWeed0907 发布于 2006-09-27 10:31, 676 次点击

table t1
aa bb cc
1 2 3
2 34 1
3 44 4
4 43 2
5 34 5


我想做查询按照 CC 排序, 但不是按升降排序
要按指定的顺序排序
比如 13425

9 回复
#2
CrazyWeed09072006-09-27 13:40

斑竹,帮我解决一下问题把

#3
CrazyWeed09072006-09-28 15:58

怎么没人理我的贴子啊,
我给大家解决问题的时候不是这样的

#4
wohemachen2006-09-28 16:00

我看了你的帖子了,可是不是很会呀~

这种排序方式有什么意义吗?

[此贴子已经被作者于2006-9-28 16:01:55编辑过]

#5
CrazyWeed09072006-09-28 17:03

没有意义,但是有用

#6
LouisXIV2006-09-28 22:19
--try,for sample,order by 1 3 4 2 5

select aa,bb,cc
from t1
order by charindex(','+rtrim(cc)+',',',1,3,4,2,5,')
#7
潇洒老乌龟2006-09-28 22:29

如何让ORDER BY按指定的顺序排序

表a里有个列叫Type,是商品类别,就3种情况:S,A,B,如下:
id name type
1 一班 S
2 五班 A
3 三班 B
4 四班 B
5 二班 A
6 六班 S
现在我需要按照‘S’,‘A’,‘B’的顺序排序,如下:
1 一班 S
6 六班 S
5 二班 A
2 五班 A
3 三班 B
4 四班 B

SELECT *
FROM tbl_test
ORDER BY "@#$$%#$%@$@#$@$@#@$这里应该咋写?"

select * from a where type='S' union all
select * from a where type='A' union all
select * from a where type='B'
select id , name ,type
from a
order by case type when 'S' then 1
when 'A' then 2 when 'B' then 3 else 4 end (如果对ID排序,则加最后加,id)
select id , name ,type
from (select *, case type when 'S' then 1 when 'A' then 2 else 3 end as seq from a) X
order by seq
上诉对ID列没有进行排序,如果在上诉基础上对ID再进行排序。
select id , name ,type(假设有列id ,name, type)
from (select *, case type when 'S' then 1 when 'A' then 2 else 3 end as seq from a) X
order by seq,id
1 一班 S
6 六班 S
2 五班 A
5 二班 A
3 三班 B
4 四班 B

#8
CrazyWeed09072006-09-29 09:19

谢了,六楼

#9
CrazyWeed09072006-09-29 09:22
以下是引用潇洒老乌龟在2006-9-28 22:29:19的发言:

如何让ORDER BY按指定的顺序排序

表a里有个列叫Type,是商品类别,就3种情况:S,A,B,如下:
id name type
1 一班 S
2 五班 A
3 三班 B
4 四班 B
5 二班 A
6 六班 S
现在我需要按照‘S’,‘A’,‘B’的顺序排序,如下:
1 一班 S
6 六班 S
5 二班 A
2 五班 A
3 三班 B
4 四班 B

SELECT *
FROM tbl_test
ORDER BY "@#$$%#$%@$@#$@$@#@$这里应该咋写?"

select * from a where type='S' union all
select * from a where type='A' union all
select * from a where type='B'
select id , name ,type
from a
order by case type when 'S' then 1
when 'A' then 2 when 'B' then 3 else 4 end (如果对ID排序,则加最后加,id)
select id , name ,type
from (select *, case type when 'S' then 1 when 'A' then 2 else 3 end as seq from a) X
order by seq
上诉对ID列没有进行排序,如果在上诉基础上对ID再进行排序。
select id , name ,type(假设有列id ,name, type)
from (select *, case type when 'S' then 1 when 'A' then 2 else 3 end as seq from a) X
order by seq,id
1 一班 S
6 六班 S
2 五班 A
5 二班 A
3 三班 B
4 四班 B

这个现在看到两种解了,一个是 select aa= case ................................end from order by aa ,我写的比较笨拙
还有一个是六楼的,他的比较好

#10
fengmumei2006-09-29 14:36

好东西不能错过,我收了

1