如何在MySQL中的两个不同表中选择相似的行(是否可能?)

时间:2010-07-30 23:32:09

标签: mysql select

我有两张桌子,我想抓住所有有同样拼写的学校或同一所学校的学校。例如:

my_table_a:

学校

Olde School        
New School    
Other, C.S. School   
Main School
Too Cool for School

my_table_b:

学校

Old School
New ES    
Other School 
Main School  
Hardknocks School

是否可以编写一个SELECT查询,该查询将在两个表中找到类似拼写的学校。有没有办法在列上使用LIKE或通配符?

如:

SELECT  my_table_a.school, my_table_b.school
FROM ` my_table_a` ,  my_table_b
WHERE  my_table_a.directory_school_name_09_10 LIKE  my_table_b.school

我用我的真实表格尝试了上述语句,我只是得到了'='会产生的内容。

基本上,我想抓住每个表格专栏中的前4个学校。 (当然,在现实世界中,我不会知道前4所学校是相似的)。

我正在尝试做什么?

4 个答案:

答案 0 :(得分:6)

对于Levenshtein Distance算法的UDF实现,您可能需要查看“codejanitor.com: Levenshtein Distance as a MySQL Stored Function”:

CREATE FUNCTION LEVENSHTEIN (s1 VARCHAR(255), s2 VARCHAR(255))
RETURNS INT
DETERMINISTIC
BEGIN
  DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
  DECLARE s1_char CHAR;
  DECLARE cv0, cv1 VARBINARY(256);
  SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
  IF s1 = s2 THEN
    RETURN 0;
  ELSEIF s1_len = 0 THEN
    RETURN s2_len;
  ELSEIF s2_len = 0 THEN
    RETURN s1_len;
  ELSE
    WHILE j <= s2_len DO
      SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
    END WHILE;
    WHILE i <= s1_len DO
      SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
      WHILE j <= s2_len DO
        SET c = c + 1;
        IF s1_char = SUBSTRING(s2, j, 1) THEN SET cost = 0; ELSE SET cost = 1; END IF;
        SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
        IF c > c_temp THEN SET c = c_temp; END IF;
        SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
        IF c > c_temp THEN SET c = c_temp; END IF;
        SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
      END WHILE;
      SET cv1 = cv0, i = i + 1;
    END WHILE;
  END IF;
  RETURN c;
END

现在让我们使用您在问题中提供的数据构建一个测试用例:

CREATE TABLE table_a (name varchar(20));
CREATE TABLE table_b (name varchar(20));

INSERT INTO table_a VALUES('Olde School');      
INSERT INTO table_a VALUES('New School');
INSERT INTO table_a VALUES('Other, C.S. School');
INSERT INTO table_a VALUES('Main School');
INSERT INTO table_a VALUES('Too Cool for School');

INSERT INTO table_b VALUES('Old School');
INSERT INTO table_b VALUES('New ES');
INSERT INTO table_b VALUES('Other School');
INSERT INTO table_b VALUES('Main School');
INSERT INTO table_b VALUES('Hardknocks School');

然后:

SELECT     *
FROM       table_a a
LEFT JOIN  table_b b ON (a.name = b.name);

显然会返回学校名称完全匹配的匹配项:

+---------------------+-------------+
| name                | name        |
+---------------------+-------------+
| Olde School         | NULL        |
| New School          | NULL        |
| Other, C.S. School  | NULL        |
| Main School         | Main School |
| Too Cool for School | NULL        |
+---------------------+-------------+
5 rows in set (0.00 sec)

现在我们可以尝试使用LEVENSHTEIN函数返回edit distance个字符不超过2个字符的学校名称:

SELECT     *
FROM       table_a a
LEFT JOIN  table_b b ON (LEVENSHTEIN(a.name, b.name) <= 2);

+---------------------+-------------+
| name                | name        |
+---------------------+-------------+
| Olde School         | Old School  |
| New School          | NULL        |
| Other, C.S. School  | NULL        |
| Main School         | Main School |
| Too Cool for School | NULL        |
+---------------------+-------------+
5 rows in set (0.08 sec)

现在使用<= 3作为编辑距离阈值:

SELECT     *
FROM       table_a a
LEFT JOIN  table_b b ON (LEVENSHTEIN(a.name, b.name) <= 3);

我们得到以下结果:

+---------------------+--------------+
| name                | name         |
+---------------------+--------------+
| Olde School         | Old School   |
| Olde School         | Other School |
| New School          | Old School   |
| Other, C.S. School  | NULL         |
| Main School         | Main School  |
| Too Cool for School | NULL         |
+---------------------+--------------+
6 rows in set (0.06 sec)

请注意,这一时间Olde School也匹配Other SchoolNew School匹配Old School。这些可能是误报,并表明定义阈值对于避免错误匹配非常重要。

解决此问题的一种常用技术是在应用阈值时考虑字符串的长度。实际上,site that I cited for this implementation还提供了LEVENSHTEIN_RATIO函数,该函数根据字符串的长度返回编辑差异的比率(以百分比表示)。

答案 1 :(得分:2)

您可以尝试比较调用SOUNDEX的结果。

或者您可以比较Levenshtein distance

答案 2 :(得分:0)

这个问题非常广泛。有什么资格类似?无论如何,虽然我不能给你一个确切的答案,但我可以说你可能想要使用全文搜索:http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html或使用LIKE通配符运算符使用一些技术:%

... LIKE "new%school"

也将匹配“新学校”和“新学校”。

答案 3 :(得分:0)

你可以尝试这个

select test1.test1name,test2.test2name from test1  join test2 on
soundex(SUBSTRING_INDEX(test1.test1name, ' ',1))=
soundex(SUBSTRING_INDEX(test2.test2name, ' ',1))

只需根据你的名字和字段名称输入。