34,590
社区成员
发帖
与我相关
我的任务
分享
怎么个特殊? 这样
select row_number(partition by 字段 order by 字段) 序号 ,* from 表
order by case when 序号=1 then 1 else when... then 2 ... else ... end
CREATE TABLE TBTEST(ID INT, subid INT,Sname CHAR(1))
INSERT TBTEST
SELECT 1 , 2 , 'a' UNION ALL
SELECT 1 , 1 , 'b' UNION ALL
SELECT 1 , 3 , 'c' UNION ALL
SELECT 1 , 4 , 'd' UNION ALL
SELECT 1 , 5 , 'f' UNION ALL
SELECT 2 , 1 , 'a' UNION ALL
SELECT 2 , 3 , 'b' UNION ALL
SELECT 3 , 2 , 'a' UNION ALL
SELECT 3 , 1 , 'b' UNION ALL
SELECT 3 , 3 , 'c' UNION ALL
SELECT 3 , 4 , 'd' UNION ALL
SELECT 3 , 5 , 'f'
--DROP TABLE TBTEST
select * from tbtest order by case when ID=2 then 1 else 0 end ,case when id=2 and sname='a' then 0 else 1 end
ID subid Sname
----------- ----------- -----
1 2 a
1 1 b
1 3 c
1 4 d
1 5 f
3 2 a
3 1 b
3 3 c
3 4 d
3 5 f
2 1 a
2 3 b
(所影响的行数为 12 行)