如何用SQL 语句查出两条记录的内容相似度最大

shaken 2006-04-13 05:33:12
如表格A中字段a1有一条字符串记录Rec1内容为 “我的家在哪里?”;然后要从B表中找出一条字符串记录Rec2内容与Rec1最相似的,如:“我的家在浙江?”
不够分继续加!!
...全文
766 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
超级大笨狼 2006-12-05
  • 打赏
  • 举报
回复
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER function get_semblance_By_2words
(
@word1 varchar(50),
@word2 varchar(50)
)
returns nvarchar(4000)
as
begin
declare @re int
declare @temp varchar(50)
declare @maxLenth int
declare @i int,@l int
declare @tb1 table(child varchar(50))
declare @tb2 table(child varchar(50))
set @i=1
set @l=2

set @maxLenth=len(@word1)
if len(@word1)<len(@word2)
begin
set @maxLenth=len(@word2)
end
-- set @word1=replace(@word1,' ','')
--set @word2=replace(@word2,' ','')
while @l<=len(@word1)
begin
while @i<len(@word1)
begin
set @temp=SUBSTRING(@word1,@i,@l)
if not exists(select child from @tb1 where child=@temp)
begin
insert @tb1 (child) values( @temp )
end
set @i=@i+1
end
set @i=1
set @l=@l+1
end


set @i=1
set @l=2


while @l<=len(@word2)
begin
while @i<len(@word2)
begin
set @temp=SUBSTRING(@word2,@i,@l)
if not exists(select child from @tb2 where child=@temp)
begin
insert @tb2 (child) values( @temp )
end
set @i=@i+1
end
set @i=1
set @l=@l+1
end
select @re=isnull(max( len(a.child)*100/ @maxLenth ) ,0) from @tb1 a, @tb2 b where a.child=b.child
return @re
end















GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

超级大笨狼 2006-12-05
  • 打赏
  • 举报
回复
select dbo.get_semblance_By_2words('我的家在哪里?','我的家在浙江?')
select dbo.get_semblance_By_2words('浙江家的我在?','我的家在浙江?')
select dbo.get_semblance_By_2words('我的家在哪里?','浙江家的我在?')
--57
--28
--0
超级大笨狼 2006-12-05
  • 打赏
  • 举报
回复
--CSDN超级大笨狼。





CREATE function get_semblance_By_2words
(
@word1 varchar(50),
@word2 varchar(50)
)
returns nvarchar(4000)
as
begin
declare @re int
declare @maxLenth int
declare @i int,@l int
declare @tb1 table(child varchar(50))
declare @tb2 table(child varchar(50))
set @i=1
set @l=2
set @maxLenth=len(@word1)
if len(@word1)<len(@word2)
begin
set @maxLenth=len(@word2)
end
while @l<=len(@word1)
begin
while @i<len(@word1)-1
begin
insert @tb1 (child) values( SUBSTRING(@word1,@i,@l) )
set @i=@i+1
end
set @i=1
set @l=@l+1
end


set @i=1
set @l=2


while @l<=len(@word2)
begin
while @i<len(@word2)-1
begin
insert @tb2 (child) values( SUBSTRING(@word2,@i,@l) )
set @i=@i+1
end
set @i=1
set @l=@l+1
end

select @re=isnull(max( len(a.child)*100/ @maxLenth ) ,0) from @tb1 a, @tb2 b where a.child=b.child
return @re
end








GO

--测试
--select dbo.get_semblance_By_2words('我是谁','我是谁啊')
--75
--相似度
shaken 2006-04-14
  • 打赏
  • 举报
回复
不同长度的字符串,libin_ftsafe(子陌红尘) 的比较结果有点偏离需求
jwt1982 2006-04-14
  • 打赏
  • 举报
回复
学习一下
撸大湿 2006-04-13
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘)

能解决次序问题啊
shaken 2006-04-13
  • 打赏
  • 举报
回复
谢谢大家的回复,
但是这里边有个次序问题,如:
“我的家在哪里?”
“我的家在浙江?”
“浙江家的我在?”
第三个字符串虽然在字符上与第一个字符串相似度也是4个字符,但是由于次序问题,是不符合要求的。用Diffrence只能区分字符的相似度,并没有按照次序来比较字符串的相似度。
xeqtr1982 2006-04-13
  • 打赏
  • 举报
回复
学习 :)
zhaoanle 2006-04-13
  • 打赏
  • 举报
回复
...
itblog 2006-04-13
  • 打赏
  • 举报
回复
第二次回复写反了~
itblog 2006-04-13
  • 打赏
  • 举报
回复
SELECT A.Rec1,max(DIFFERENCE(A.Rec1, B.Rec2)) as Rec2 from 表格A A,表格B B
itblog 2006-04-13
  • 打赏
  • 举报
回复
SELECT A.Rec1,min(DIFFERENCE(A.Rec1, B.Rec2)) as Rec2 from 表格A A,表格B B
子陌红尘 2006-04-13
  • 打赏
  • 举报
回复
不深究细节,提供一个最简单的比较函数:
-------------------------------------------------------------------------------------------------------------------------------
create function F_StrCompare(@str1 varchar(8000),@str2 varchar(8000))
returns int
as
begin
declare @cnt int,@num int
select @cnt=0,@num=(case when len(@str1)>len(@str2) then len(@str1) else len(@str2) end)

while @num>0
begin
if substring(@str1,@num,1)=substring(@str2,@num,1)
set @cnt=@cnt+1
set @num=@num-1
end
return @cnt
end
go

declare @t table(id int,[desc] varchar(1000))
insert into @t select 1,'我的家在这里?'
insert into @t select 1,'我的家在浙江?'

select top 1 * from @t order by dbo.F_StrCompare('我的家在哪里?',[desc]) desc

/*
id desc
----------- -----------------------
1 我的家在这里?
*/

drop function F_StrCompare
edp08 2006-04-13
  • 打赏
  • 举报
回复
楼上快手

DIFFERENCE 函数比较两个字符串的 SOUNDEX 值,并评估它们之间的相似性,最后返回 0 到 4 之间的一个值,其中 4 表示匹配性最高。
huailairen 2006-04-13
  • 打赏
  • 举报
回复
如表格A中字段a1有一条字符串记录Rec1内容为 “我的家在哪里?”;然后要从B表中找出一条字符串记录Rec2内容与Rec1最相似的,如:“我的家在浙江?”
select A.a1, max(DIFFERENCE(A.a1,B.相应字段))
from A cross join B
group by A.a1
itblog 2006-04-13
  • 打赏
  • 举报
回复
SELECT DIFFERENCE('我的家在哪里?', '我的家在浙江?')

22,209

社区成员

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

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