-1

想象一张这样的桌子:

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 做到这一点?

4

2 回答 2

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))

于 2012-08-21T08:11:01.827 回答
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 是否包含在任何其他字符串中)。您必须例如从第二个字符串中删除匹配的字符以防止这种行为;我将把它作为练习留给感兴趣的读者。

于 2012-08-21T08:33:52.580 回答