按给定条件排序

blueyund 2008-10-10 05:04:05
比如表
T1(n1,n2)
a 1
a 1
b 1
c 1
f 1
g 1
g 1
a 1

我要n1中的g先显示在显示c的,然后剩下的再按正常的降序或升序排
如何实现呢
...全文
88 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueyund 2008-10-10
  • 打赏
  • 举报
回复
大家好强哈
这么快就搞定了
davidchen0218 2008-10-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pt1314917 的回复:]
SQL code

select * from t1 order by case n1 when 'g' then 0 when 'c' then 1 else 2 end
[/Quote]

order by 还没这么写过.学习了。
davidchen0218 2008-10-10
  • 打赏
  • 举报
回复

declare @T1 table(n1 varchar(5),n2 int)
insert into @T1
select 'a',1 union all
select 'c',1 union all
select 'b',1 union all
select 'd',1 union all
select 'f',1 union all
select 'g',1 union all
select 'g',2 union all
select 'a',1


select *,'n3'=(case n1 when 'g' then 2 when 'c' then 1 end),'n4'=(case when n1 ='g' or n1 ='c' then 1 else 2 end) from @T1
order by n4 asc,n3 desc,n1 asc

/*
n1 n2 n3 n4
--------------------
g 1 2 1
g 2 2 1
c 1 1 1
a 1 NULL 2
a 1 NULL 2
b 1 NULL 2
d 1 NULL 2
f 1 NULL 2
*/


pt1314917 2008-10-10
  • 打赏
  • 举报
回复


select * from t1 order by case n1 when 'g' then 0 when 'c' then 1 else 2 end
tim_spac 2008-10-10
  • 打赏
  • 举报
回复
order by case n1
when 'g' then '0'
when 'c' then '1'
else '9' end + n1
blueyund 2008-10-10
  • 打赏
  • 举报
回复
比如校内网
在里面搜索人
他的排序是
1.你目前所在学校
2.你就读过的学校(高中,初中,小学依次下来)
后面好像是
3.你好友所在学校
blueyund 2008-10-10
  • 打赏
  • 举报
回复
要求不是用联合查询
即不是先查n1中为g的,再查为f的,最后查非g非f的,再把3次结果连接
不用这种方法能实现吗

有哪位有过类似要求的应用吗
是用什么方法

22,209

社区成员

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

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