高分求助,关于order by排序 select * from tablename order by a2,a4 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 order by charindex(title,'a2a4a3a1a5') CHARINDEX返回字符串中指定表达式的起始位置。 语法CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数expression1一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。expression2一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。start_location在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索。 --测试:create table t1(id int,title char(2))insert into t1 select '1','a1'insert into t1 select '2','a2'insert into t1 select '3','a3'insert into t1 select '4','a4'insert into t1 select '5','a5'select * from t1 order by charindex(title,'a2a4a3a1a5')/*id title ----------- ----- 2 a24 a43 a31 a15 a5(所影响的行数为 5 行)*/ order by case title when 'a2' then 0 when 'a4' then 1 when 'a3' then 2 else 3 end,id /*--补充一下 马可的方法,需要将所有的title列出 如果楼主只是按照:title = 'a2' or title = 'a4' or title = 'a3' 其余的排在后面,并按title从大到小排序,或不排序 就用下面的处理方法--*/declare @t1 table(id int,title char(2))insert into @t1 select '1','a1'insert into @t1 select '2','a2'insert into @t1 select '3','a3'insert into @t1 select '4','a4'insert into @t1 select '5','a5'select * from @t1 order by charindex(title,'a3a4a2') desc --先按指定顺序排,注意,与马可的不同,是反过来写 ,title --如果其余的要排序,就加此句,否则不要此句./*--测试结果id title ----------- ----- 2 a24 a43 a31 a15 a5(所影响的行数为 5 行)--*/ order by charindex(','+title+',',',a3,a4,a2,') desc否则a3,a4,a2内容相互包含就不对了. 蒋老师的最完整:或者干脆用 case when 这种要求的贴子有一段时间经常出现的,不过这一阵没怎么见到了。 帮忙看下这个SQL语句有什么问题 求助 sql 多表查询 大侠帮我分析一下以下代码为什么性能差别如此之大 根据表中的一个字段,如何给另一个字段赋值 使用时raiserror遇到的特殊问题。 请教sql语句 sqlserver 6.5 的数据库中出现删除不掉的用户表,如何处理? 请问库存盘点是怎么一回事,那位高手可以跟我讲讲,多谢谢!! 方案讨论(有些难度哟!) Sql复制订阅相关数据表数据存在哪里? 请问为什么有的windows2k能装mssql,而有的只能装mssql的客户端? 这个SQL怎么写
返回字符串中指定表达式的起始位置。 语法
CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数
expression1一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。expression2一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。start_location在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索。
create table t1(id int,title char(2))
insert into t1 select '1','a1'
insert into t1 select '2','a2'
insert into t1 select '3','a3'
insert into t1 select '4','a4'
insert into t1 select '5','a5'select * from t1 order by charindex(title,'a2a4a3a1a5')
/*
id title
----------- -----
2 a2
4 a4
3 a3
1 a1
5 a5(所影响的行数为 5 行)*/
when 'a4' then 1
when 'a3' then 2
else 3 end,id
如果楼主只是按照:title = 'a2' or title = 'a4' or title = 'a3'
其余的排在后面,并按title从大到小排序,或不排序
就用下面的处理方法--*/declare @t1 table(id int,title char(2))
insert into @t1 select '1','a1'
insert into @t1 select '2','a2'
insert into @t1 select '3','a3'
insert into @t1 select '4','a4'
insert into @t1 select '5','a5'select * from @t1
order by charindex(title,'a3a4a2') desc --先按指定顺序排,注意,与马可的不同,是反过来写
,title --如果其余的要排序,就加此句,否则不要此句./*--测试结果
id title
----------- -----
2 a2
4 a4
3 a3
1 a1
5 a5(所影响的行数为 5 行)
--*/
order by charindex(','+title+',',',a3,a4,a2,') desc
否则a3,a4,a2内容相互包含就不对了.
或者干脆用 case when 这种要求的贴子有一段时间经常出现的,不过这一阵没怎么见到了。