27,579
社区成员
发帖
与我相关
我的任务
分享
select *
from A
order by B ,case when B = 0 then C else 1 end,case when B = 0 then D else 1 end,
case when B = 1 then C else 0 end desc,case when B = 1 then D else 0 end desc
drop table A
/*
id B C D
-------------------------
1 0 1 2
2 0 1 3
7 0 1 4
6 0 2 1
5 1 4 4
3 1 3 6
4 1 3 5
*/
create table A (
id char(1),
B char(1),
C int,
D int
)
insert into A values('1','0',1,2)
insert into A values('2','0',1,3)
insert into A values('3','1',3,6)
insert into A values('4','1',3,5)
insert into A values('5','1',4,4)
insert into A values('6','0',2,1)
insert into A values('7','0',1,4)
select *
from A
order by B desc,case when B = 0 then C else 1 end,case when B = 0 then D else 1 end,
case when B = 1 then C else 0 end desc,case when B = 1 then D else 0 end desc
drop table A
/*
id B C D
-------------------------
5 1 4 4
3 1 3 6
4 1 3 5
1 0 1 2
2 0 1 3
7 0 1 4
6 0 2 1
*/
select *
from tb
order by case when B = b1 then C else 0 end,case when B = b1 then D else 0 end,
case when B = b2 then C else 0 end desc,case when B = b2 then D else 0 end desc