简体   繁体   中英

SQL - Select ALL rows that are LIKE%…% of each other (All Similar Rows)

For example, lets say in column source I have the following entries

SourceFileEmbed122
SourceFile1333
SourceItem13366
PreLoadSource7755

And I do a query of SourceFile it should match row 1 and 2 and show me all the column data for that row, but if I search for example: PreLoadSource or SourceItem it shouldnt show anything, as there is only 1 row that has a similar entry.

Kinda like an if contains sort of thing.

Basically, I want to do something like: SELECT source, COUNT(*) TotalCount FROM sources GROUP BY source HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC

But the query does LIKE instead of LIKE%...% (Like in PHPMyAdmin) which results in it only matching EXACT matches of each other, so stuff like:

row123/
row123

Wont match each other and will be ignored. But I want this to MATCH basically if row123's full text is ALSO all in another row's value, then match.

Lets say I have:

http://link.ext/dir123/file.mp3
http://link.ext/dir123
http://link.ext/dir123/file2.mp3
http://link.ext/dir123

The query should match .../file.mp3 , .../file2.mp3 and ../dir123 because row 2 http://link.ext/dir123 is also in row 1, 3 and 4.

One way to test for at least two matches is:

select s.*
from sources s
where s.source like '%<whatever>%' and
      exists (select 1
              from source s2
              where s2.source like '%<whatever>%' and
                    s2.source <> s.source
             );

One way is doing a inner join with the same table, if you need a simple count you can do something like that:

SELECT s1.source, COUNT(*)
FROM sources s1
INNER JOIN sources s2
    ON s1.id <> s2.id AND s1.source LIKE CONCAT('%', s2.source, '%')
GROUP BY s1.source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM