如何让某一个固定名称的字段始在前面显示?

人生无回头路 2007-07-04 01:30:27
如我想让表1中的NAME字段为张三的始终在前面显示
...全文
239 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sp4 2007-07-04
  • 打赏
  • 举报
回复
order by case when name='张三' then 0 else 1 end asc 最合题意

charindex(name,'张三') desc 有误差
name 是'张' 和 '张三' 那结果是一样的,有可能导致张排在前面
如果name是'三',就肯定会排在张三前面
mengmou 2007-07-04
  • 打赏
  • 举报
回复
--借用一下数据
create table test(name nvarchar(20))
insert test select N'赵六'
union all select N'李四'
union all select N'张三'
union all select N'王五'

select name from test
order by charindex(name,'张三') desc

drop table test
/*
name
--------------------
张三
王五
赵六
李四

(4 row(s) affected)
*/
mengmou 2007-07-04
  • 打赏
  • 举报
回复
反了
order by charindex(NAME,'张三') desc
bill024 2007-07-04
  • 打赏
  • 举报
回复
这么多回复了!!!
xmlquit 2007-07-04
  • 打赏
  • 举报
回复
select * from (select a.*,type = '1' from a where name = '張三' union all
select a.*,type = '0' from a where name <> '張三') order by desc
bill024 2007-07-04
  • 打赏
  • 举报
回复
这样?
create table test(name nvarchar(20))
insert test select N'赵六'
union all select N'李四'
union all select N'张三'
union all select N'王五'

select name from test
order by case when name=N'张三' then 0
else 1 end

drop table test

name
--------------------
张三
王五
赵六
李四
mengmou 2007-07-04
  • 打赏
  • 举报
回复
order by charindex('张三',NAME) desc
昵称被占用了 2007-07-04
  • 打赏
  • 举报
回复
select *
from 表1
order by case when name='张三' then 0 else 1 end,其他排序字段
zsforever 2007-07-04
  • 打赏
  • 举报
回复
order by case when name='张三' then 0 else 1 end asc
whisht 2007-07-04
  • 打赏
  • 举报
回复
不明白。

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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