Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Q:如何在SQL中的不同表中查找相同和相似的字符串值_Sql_Sql Server - Fatal编程技术网

Q:如何在SQL中的不同表中查找相同和相似的字符串值

Q:如何在SQL中的不同表中查找相同和相似的字符串值,sql,sql-server,Sql,Sql Server,我的数据库中有两个表,每个表都有带名称的列。如何比较这些表列,如何找到这些名称,哪些名称与表1和表2中的名称完全匹配 例如: 表1 column1 --------------------- Tom Hawky Antony Hendric Formen Cathy Cassy Bill Gates Mary Diore 表2 column1 ---------------------- Christopher Fridricson Ken Lovely Tom Hawky Anthony F

我的数据库中有两个表,每个表都有带名称的列。如何比较这些表列,如何找到这些名称,哪些名称与表1和表2中的名称完全匹配

例如:

表1

column1
---------------------
Tom Hawky
Antony Hendric Formen
Cathy Cassy
Bill Gates
Mary Diore
表2

column1
----------------------
Christopher Fridricson
Ken Lovely
Tom Hawky
Anthony Foreman
Chati Cassei
结果应该是这样的:

table1 - table2
Tom Hawky - Tom Hawky
Antony Hendric Formen - Anthony Henrich Foreman
Cathy Cassy - Chati Cassei

您尚未明确定义用于确定相似性的参数。但是一个有趣的方法可能是使用
差异
函数。这将比较两个字符串的语音表示(使用
SOUNDEX
函数),并返回0到4之间的值,其中4是最强匹配。因此,您可以尝试以下方法:

SELECT t1.column1 + ' - ' + t2.column1 AS 'table1 - table2'
FROM table1 t1
INNER JOIN table2 t2
ON DIFFERENCE(t1.column1,t2.column1)>= 3
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = false)]
public static int Levenshtein(SqlString S1, SqlString S2)
{
    if (S1.IsNull)
        S1 = new SqlString("");

    if (S2.IsNull)
        S2 = new SqlString("");

    int maxLen = 4096;

    // keeping only the first part of the string (performance reasons)
    String SC1 = S1.Value.ToUpper();
    String SC2 = S2.Value.ToUpper();

    if (SC1.Length > maxLen)
        SC1 = SC1.Remove(maxLen);
    if (SC2.Length > maxLen)
        SC2 = SC2.Remove(maxLen);

    int n = SC1.Length;
    int m = SC2.Length;

    short[,] d = new short[n + 1, m + 1];
    int cost = 0;

    if (n + m == 0)
    {
        return 0;
    }
    else if (n == 0)
    {
        return 0;
    }
    else if (m == 0)
    {
        return 0;
    }

    for (short i = 0; i <= n; i++)
        d[i, 0] = i;

    for (short j = 0; j <= m; j++)
        d[0, j] = j;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (SC1[i - 1] == SC2[j - 1])
                cost = 0;
            else
                cost = 1;

            d[i, j] = (short) System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
        }
    }

    // double percentage = System.Math.Round((1.0 - ((double)d[n, m] / (double)System.Math.Max(n, m))) * 100.0, 2);
    // return percentage;
    return d[n, m];
}

-- SQL to actually create scalar function that calls CLR code
ALTER FUNCTION dbo.Levenshtein(@S1 nvarchar(max), @S2 nvarchar(max))
    RETURNS INT as EXTERNAL NAME ClrUtils.StoredFunctions.Levenshtein
GO

-- CLR must be enabled for database
sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO

我已将最低比较级别设置为3,但您可以调整它以最适合您的数据

一种解决方案是执行以下操作:

1) 将名称拆分为一些临时表,这样比较就容易多了

2) 构造一个查询,尝试根据某个距离查找匹配项

1)拆分成单词

drop table #T1Words
Go

create table #T1Words (Id1 INT NOT NULL, Word NVARCHAR(100))
GO

insert into #T1Words 
select T1.Id1, X1.Item
from Table1 T1
    cross apply dbo.SplitStrings_XML (T1.Column1, N' ') X1
GO

drop table #T2Words
GO 

create table #T2Words (Id2 INT NOT NULL, Word NVARCHAR(200))
GO

insert into #T2Words 
select T2.Id2, X2.Item
from Table2 T2
    cross apply dbo.SplitStrings_XML (T2.Column2, N' ') X2
GO
设置

-- drop table Table1
create table Table1 
(
    Id1 INT NOT NULL CONSTRAINT PK_Table1 PRIMARY KEY IDENTITY (1, 1),
    Column1 NVARCHAR(200) NOT NULL
)
GO

insert into Table1 (Column1)
VALUES ('Tom Hawky'), ('Antony Hendric Formen'), ('Cathy Cassy'), ('Bill Gates'), ('Mary Diore')
GO

-- drop table Table2
create table Table2
(
    Id2 INT NOT NULL CONSTRAINT PK_Table2 PRIMARY KEY IDENTITY (1, 1),
    Column2 NVARCHAR(200) NOT NULL
)
GO

insert into Table2 (Column2)
VALUES ('Christopher Fridricson'), ('Ken Lovely'), ('Tom Hawky'), ('Anthony Foreman'), ('Chati Cassei'), ('Tom X')
GO

select * from Table1
GO

select * from Table2
GO
分割功能

有几种方法可以使用,我选择了XML方式:

CREATE FUNCTION dbo.SplitStrings_XML
(
   @List       NVARCHAR(MAX),
   @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
   RETURN 
   (  
      SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
      FROM 
      ( 
        SELECT x = CONVERT(XML, '<i>' 
          + REPLACE(@List, @Delimiter, '</i><i>') 
          + '</i>').query('.')
      ) AS a CROSS APPLY x.nodes('i') AS y(i)
   );
GO
使用一定距离进行选择

可以使用的一个距离是。为了在SQL中使用它,它必须在CLR中实现,否则速度会非常慢。大概是这样的:

SELECT t1.column1 + ' - ' + t2.column1 AS 'table1 - table2'
FROM table1 t1
INNER JOIN table2 t2
ON DIFFERENCE(t1.column1,t2.column1)>= 3
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = false)]
public static int Levenshtein(SqlString S1, SqlString S2)
{
    if (S1.IsNull)
        S1 = new SqlString("");

    if (S2.IsNull)
        S2 = new SqlString("");

    int maxLen = 4096;

    // keeping only the first part of the string (performance reasons)
    String SC1 = S1.Value.ToUpper();
    String SC2 = S2.Value.ToUpper();

    if (SC1.Length > maxLen)
        SC1 = SC1.Remove(maxLen);
    if (SC2.Length > maxLen)
        SC2 = SC2.Remove(maxLen);

    int n = SC1.Length;
    int m = SC2.Length;

    short[,] d = new short[n + 1, m + 1];
    int cost = 0;

    if (n + m == 0)
    {
        return 0;
    }
    else if (n == 0)
    {
        return 0;
    }
    else if (m == 0)
    {
        return 0;
    }

    for (short i = 0; i <= n; i++)
        d[i, 0] = i;

    for (short j = 0; j <= m; j++)
        d[0, j] = j;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (SC1[i - 1] == SC2[j - 1])
                cost = 0;
            else
                cost = 1;

            d[i, j] = (short) System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
        }
    }

    // double percentage = System.Math.Round((1.0 - ((double)d[n, m] / (double)System.Math.Max(n, m))) * 100.0, 2);
    // return percentage;
    return d[n, m];
}

-- SQL to actually create scalar function that calls CLR code
ALTER FUNCTION dbo.Levenshtein(@S1 nvarchar(max), @S2 nvarchar(max))
    RETURNS INT as EXTERNAL NAME ClrUtils.StoredFunctions.Levenshtein
GO

-- CLR must be enabled for database
sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,IsPrecise=false)]
公共静态int-Levenshtein(sqlstrings1、sqlstrings2)
{
if(S1.IsNull)
S1=新的SqlString(“”);
if(S2.IsNull)
S2=新的SqlString(“”);
int-maxLen=4096;
//仅保留字符串的第一部分(性能原因)
字符串SC1=S1.Value.ToUpper();
字符串SC2=S2.Value.ToUpper();
如果(SC1.Length>maxLen)
SC1=SC1。移除(maxLen);
如果(SC2.Length>maxLen)
SC2=SC2.移除(maxLen);
int n=SC1.长度;
int m=SC2.长度;
短[,]d=新短[n+1,m+1];
整数成本=0;
如果(n+m==0)
{
返回0;
}
else如果(n==0)
{
返回0;
}
else如果(m==0)
{
返回0;
}

对于(缩写i=0;i“相似”是什么意思?在任何人回答您的问题之前,您需要一个非常具体的定义。查找精确匹配很容易,即使忽略大小写也很容易,但查找相似的名称是一个全新的难题。对您的代码进行一点解释将大大改进您的答案。