请问这个排序怎么做?

Kingofcode 2011-09-23 11:19:38
我有两列数据

ID name
---------------------------------
1 0301
3 0202
4 0306
5 0904
6 0905
7 0402
8 1101


由于0301比较特殊
现在我想要的结果是 除了0301要按name来排 其他的都按照ID来排

结果为

ID name
---------------------------------
3 0202
1 0301
4 0306
5 0904
6 0905
7 0402
8 1101

谢谢先
...全文
257 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2011-09-23
  • 打赏
  • 举报
回复
create table tb(ID int,name varchar(10))
insert into tb select 1,'0301'
insert into tb select 3,'0202'
insert into tb select 4,'0306'
insert into tb select 5,'0904'
insert into tb select 6,'0905'
insert into tb select 7,'0402'
insert into tb select 8,'1101'
GO
SELECT * FROM TB ORDER BY CASE WHEN NAME='0301' THEN 0 ELSE 1 END ASC,NAME asc
jalor_6 2011-09-23
  • 打赏
  • 举报
回复
如果数据多了,是不是0301这行一直就排第二,不会有其它可能?
jalor_6 2011-09-23
  • 打赏
  • 举报
回复
哦,不好意思,还有0402在,没看清,见笑了
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fredrickhu 的回复:]
order by case when name='0301' then 1 else 0 end ,id
[/Quote]也不行

jalor_6 2011-09-23
  • 打赏
  • 举报
回复
很简单,把name转为数字型,排序就OK了

select id,name from tb order by convert(int,name)
--小F-- 2011-09-23
  • 打赏
  • 举报
回复
order by case when name='0301' then 1 else 0 end ,id
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qianjin036a 的回复:]
SQL code
create table tb(ID int,name varchar(10))
insert into tb select 1,'0301'
insert into tb select 3,'0202'
insert into tb select 4,'0306'
insert into tb select 5,'0904'
insert into tb selec……
[/Quote] 这种如果是固定这几条数据就可以这么写 可是name不是固定的 id和name的对应自然也不是固定的
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]
SQL code

order by (case when [name] = '0301' then [name] else ltrim(id) end)



???
[/Quote]这个不行
-晴天 2011-09-23
  • 打赏
  • 举报
回复
create table tb(ID int,name varchar(10))
insert into tb select 1,'0301'
insert into tb select 3,'0202'
insert into tb select 4,'0306'
insert into tb select 5,'0904'
insert into tb select 6,'0905'
insert into tb select 7,'0402'
insert into tb select 8,'1101'
go
select * from tb order by (case when id=3 then -id else id end)
/*
ID name
----------- ----------
3 0202
1 0301
4 0306
5 0904
6 0905
7 0402
8 1101

(7 行受影响)

*/
go
drop table tb

AcHerat 2011-09-23
  • 打赏
  • 举报
回复

order by (case when [name] = '0301' then [name] else ltrim(id) end)


???
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
都吃饭去了吗?
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]
怎么确定的0301所在行在0202之后。
[/Quote]
只有0301 按name排序
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
跌的好快 ID NAME 不是固定的 求帮助
AcHerat 2011-09-23
  • 打赏
  • 举报
回复
怎么确定的0301所在行在0202之后。
yubofighting 2011-09-23
  • 打赏
  • 举报
回复
不是已经搞定了吗,要么就是你需求不明确
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
这个功能搞不定吗?各位两点了 上班了
Kingofcode 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 roy_88 的回复:]
SQL code
create table tb(ID int,name varchar(10))
insert into tb select 1,'0301'
insert into tb select 3,'0202'
insert into tb select 4,'0306'
insert into tb select 5,'0904'
insert into tb selec……
[/Quote]也不对 回13楼 都不确定 只是0301要处在它按NAME排序的位置上 其他的全部按ID排序

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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