22,209
社区成员
发帖
与我相关
我的任务
分享
ID name
---------------------------------
1 0301
3 0202
4 0306
5 0904
6 0905
7 0402
8 1101
ID name
---------------------------------
3 0202
1 0301
4 0306
5 0904
6 0905
7 0402
8 1101
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
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
order by (case when [name] = '0301' then [name] else ltrim(id) end)