select语句能按where条件排序么?

forever_ylx 2009-11-23 07:42:16
select a from 表 where (a='xx' or a='yy' or a='zz')

结果:
a
----
xx
yy
zz


select a from 表 where (a='xx' or a='zz' or a='yy')

结果:
a
----
xx
zz
yy

select语句能按where条件排序么?该怎么写呢?
...全文
237 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanglinqiang 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fredrickhu 的回复:]
SQL codeselect
afrom
tbwhere
a='xx'or a='zz'or a='yy'orderbycharindex(a,'xx,zz,yy')
[/Quote]
这个好……
yanglinqiang 2009-11-23
  • 打赏
  • 举报
回复

select a from 表 where a="xx"
union all select a from 表 where a="zz"
union all select a from 表 where a="yy"
huangqing_80 2009-11-23
  • 打赏
  • 举报
回复

declare @mytb table(col1 char(3),col2 char(2),col3 decimal(3,0))
insert @mytb select 'xx','ac',3 union
select 'yy','ad',5 union
select 'zz','ab',6 union
select 'aa','b4',7 union
select 'bb','a4',8
select * from @mytb where (col2='ab' or col2='ac' or col2='ad') order by col2
--小F-- 2009-11-23
  • 打赏
  • 举报
回复
select
a
from
tb
where
a='xx' or a='zz' or a='yy'
order by
charindex(a,'xx,zz,yy')
zmx0506 2009-11-23
  • 打赏
  • 举报
回复
大家太有才了,能写出这么多花样来
icelovey 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 feixianxxx 的回复:]
SQL codedeclare@avarchar(10),@bvarchar(10),@cvarchar(10),@svarchar(8000)set@a='xx'set@b='zz'set@c='yy'set@s='select a from 表 where ( a='''+@a+''' or a='''+@b+''' or a='''+@c+''')'+' order by charindex(a,''@a+@b+@c'')'exec (@s)
通过 @a @b @c 控制
[/Quote]
UP
feixianxxx 2009-11-23
  • 打赏
  • 举报
回复
declare @a varchar(10),@b varchar(10),@c varchar(10),@s varchar(8000)
set @a='xx'
set @b='zz'
set @c='yy'
set @s='select a from 表 where ( a='''+@a+''' or a='''+@b+''' or a='''+@c+''')'
+' order by charindex(a,''@a+@b+@c'')'
exec (@s)

通过 @a @b @c 控制
dawugui 2009-11-23
  • 打赏
  • 举报
回复
select a from 表 where (a='xx' or a='yy' or a='zz') 
order by a
pt1314917 2009-11-23
  • 打赏
  • 举报
回复

select a from 表 where (a='xx' or a='zz' or a='yy')
order by case a when 'xx' then 1 when 'zz' then 2 else 3 end
SQL77 2009-11-23
  • 打赏
  • 举报
回复
select a from 表 where (a='xx' or a='yy' or a='zz') 
ORDER BY CHARINDEX(A,'XX,ZZ,YY')
SQL77 2009-11-23
  • 打赏
  • 举报
回复
select a from 表 where (a='xx' or a='yy' or a='zz') 
ORDER BY A
?
  • 打赏
  • 举报
回复
select a from 表 where (a='xx' or a='zz' or a='yy')
order by charindex(a,'xx,zz,yy')

34,873

社区成员

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

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