SQLite fuzzy duplicate search using LIKE

蹲街弑〆低调 提交于 2020-01-23 21:28:28

问题


I have a table with 4 entries.

    CREATE TABLE tab( 
    name Text 
                     ); 

    INSERT INTO "tab" VALUES('Intertek');
    INSERT INTO "tab" VALUES('Pntertek');
    INSERT INTO "tab" VALUES('Ontertek');
    INSERT INTO "tab" VALUES('ZTPay');

Pntertek & Ontertek are fuzzy duplicates of the correctly spelt Intertek. I wish to create a list consisting of fuzzy duplicates and the correctly spelt names. However, I don't want the list to contain the correctly spelt name if there is no fuzzy duplicate found by the LIKE search.

The following line tells me how many entries match my fuzzy search criteria:

    SELECT COUNT(name) 
    FROM tab 
    WHERE name LIKE '%ntertek' ;

    SELECT COUNT(name) 
    FROM tab 
    WHERE name LIKE '%TPay' ;

This works fine and gives 3 and 1 respectively.

I know this next part is wrong but it expresses what I want to happen:

    SELECT name 
    FROM tab 
    WHERE name LIKE '%ntertek'
    GROUP BY name 
    HAVING COUNT(name) FROM tab WHERE name LIKE '%ntertek' > 1 ; 

    SELECT name 
    FROM tab 
    WHERE name LIKE '%TPay'
    GROUP BY name 
    HAVING COUNT(name) FROM tab WHERE name LIKE '%TPay' > 1 ; 

In my mind this should create the following list: Intertek, Ontertek, Entertek.

But I get a near "FROM": syntax error.

I'm somewhat of a novice with sql and programming in general so any help would be greatly appreciated.

Thanks in advance for any help.


回答1:


If you use GROUP BY name, you will get a separate group for every distinct name, and then you will not be able to count similar names.

You should use a subquery:

SELECT name
FROM tab
WHERE name LIKE '%ntertek'
  AND (SELECT COUNT(*)
       FROM tab
       WHERE name LIKE '%ntertek') > 1



回答2:


I believe you are looking for SELECT DISTINCT

SELECT DISTINCT name
FROM tab
WHERE name LIKE '%ntertek';


来源:https://stackoverflow.com/questions/17532083/sqlite-fuzzy-duplicate-search-using-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!