举个例子说明:
select * from table1 where 字段1 like ''c''
这个查询执行后,所有饱含‘a’的记录都会检索出来,例如ac,bac,cd,c等,如果按照字段1进行排序,可能的顺序就是
ac
bac
c
cd现在我希望实现的功能是:如果查询条件是‘c’,那么‘c’就排在最前面,也就是结果中,越相近的越考前,例如,‘c’就比‘ac’考前,而‘ac’又比‘bac’靠前请问有办法实现吗?
select * from table1 where 字段1 like ''c''
这个查询执行后,所有饱含‘a’的记录都会检索出来,例如ac,bac,cd,c等,如果按照字段1进行排序,可能的顺序就是
ac
bac
c
cd现在我希望实现的功能是:如果查询条件是‘c’,那么‘c’就排在最前面,也就是结果中,越相近的越考前,例如,‘c’就比‘ac’考前,而‘ac’又比‘bac’靠前请问有办法实现吗?
解决方案 »
- 如何调整黑白图片的gama(伽玛)值?
- can't perform this operation on an open database
- 皮皮散分系列之一<老树>
- 在suiForm上放置的suiMainMemu菜单中自动出现的字符怎么样才能去掉?
- 保存一个工程,最少需要保存哪些后缀名的文件?
- 求算法
- 关于delphi+dbase做物理删除的问题.........
- 问题解决 立刻加200分,高手请进
- 请问打印怎么才能按照给定的页数范围进行打印,如从第3页到第8页?
- ====把报表打印到文件======
- 其它程序在dephi运行表单上飞过后,表单出现色块,如何避免?
- .dcu文件改名,現在提示找不到.dcu文件,該怎么辦?
select * from table1 where 字段1 like '%c%' order by len(字段1) asc
更正LZ一个问题:select * from table1 where 字段1 like ''c''
--等价于
select * from table1 where 字段1=''c''
set nocount on
create table test(f varchar(20))
insert into test select 'ac'
insert into test select 'bac'
insert into test select 'c'
insert into test select 'cd'
go
--测试
select * from test
where f like '%c%'
order by charindex('c',f)--删除测试环境
drop table test
set nocount off
这样做出错,提示为:列前缀‘edtJC’与查询中所用的表名或别名不匹配,,,,
是不是不能使用变量??
当输入‘a’的时候,可能会查询出‘a','ab','dab',等等,我希望'a'是排在最前面的,'ab'在其次,'dba'在最后,也就是说,和输入的越相似越靠前
输入的是'a',选出的记录有'da''abcde',正确的排序应该是'da','abcde',如果使用charindex,排序就会成为'abcde','da',因为'a'在'ba'中的起始位置是1,而在'abcde'中的起始位置是0
set nocount on
create table test(f varchar(20))
insert into test select 'acdf'
insert into test select 'bac'
insert into test select 'csffa'
insert into test select 'cd'
go
--测试--建立自定义函数,返回相似程度
create function f_getcount(@c varchar(1000),@str varchar(8000))
returns float
as
begin
declare @ret float
declare @step int
declare @temp varchar(8000)
set @temp=replace(@str,'_','%')
set @temp=replace(@temp,@c,'_')
set @ret=0
set @step=0
while @step<=len(@temp)
begin
set @step=@step+1
if substring(@temp,@step,1)='_'
set @ret=@ret+1
end
set @ret=@ret*len(@c)/len(@str)
return @ret
end
goselect * from test
where f like '%a%'
order by dbo.f_getcount('a',f) desc
--删除测试环境
drop table test
drop function f_getcount
set nocount off--结果
/*
bac
acdf
csffa
*/