想象一下这样的表:
ID - NAME
1 BART
2 LISA
3 HOMER
4 MISA
5 HOMRE
我想只选择3个或更多字符等于和跟随的行。所以在选择之后我应该得到:
ID - NAME
2 LISA
3 HOMER
4 MISA
5 HOMRE
如何使用sql server 2008做到这一点?
答案 0 :(得分:1)
我假设你在同一个位置意味着相同的字符
这不会特别快,但是......
;with cteLetters AS
(
SELECT
ID,
#t.name,
Number as Position,
SUBSTRING(#t.name,Number,1) AS [Char]
FROM #t
INNER JOIN master.dbo.spt_values
ON Number BETWEEN 1 AND LEN(#t.name)
AND type='P'
)
select a1.name, a2.name
from cteLetters a1
inner join cteLetters a2
on a1.id<>a2.id
and a1.position = a2.position -- omit this line if order doesn't matter
and a1.char = a2.char
group by a1.name,a2.name
having COUNT(*)>=3
(我想你的桌子名为#t,有两个字段(id,name))
答案 1 :(得分:0)
要测试任意位置的常用字符,可以使用SQL函数:
create function dbo.CommonChars(@str1 varchar(255), @str2 varchar(255))
returns int
begin
declare @count int
declare @cur int
set @cur = 1
set @count = 0
while @cur <= LEN(@str1)
begin
if charindex(substring(@str1, @cur, 1), @str2) > 0
begin
set @count = @count + 1
end
set @cur = @cur + 1
end
return @count
end
测试数据:
create table #test (
id int,
name varchar(255)
);
insert into #test
select 1, 'BART' union all
select 2, 'LISA' union all
select 3, 'HOMER' union all
select 4, 'MISA' union all
select 5, 'HOMRE'
测试声明:
select * from #test t1 where exists
(select 1 from #test t2
where t1.id != t2.id and
dbo.CommonChars(t1.name, t2.name) >= 3)
返回:
id name
2 LISA
3 HOMER
4 MISA
5 HOMRE
注意:如果其中一个字符串出现多个字符,则该函数将无法按预期工作(例如,如果您有字符串“MUMMY”和“MA”,“MUMMY”将由上面的查询返回;它具有3个“共同”的Ms与“MA”,因为如果它包含在任何其他字符串中,则该函数单独检查每个M)。你必须例如从第二个字符串中删除匹配的字符以防止此类行为;我会把它留作感兴趣读者的练习。