[英]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 例如,假设在列
source
我有以下条目
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. 我对
SourceFile
进行查询,它应该匹配第1行和第2行,并向我显示该行的所有列数据,但是如果我搜索例如: PreLoadSource
或SourceItem
它应该不显示任何内容,因为只有1行具有相似的内容条目。
Kinda like an if contains sort of thing. 有点像if包含某种东西。
Basically, I want to do something like: SELECT source, COUNT(*) TotalCount FROM sources GROUP BY source HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC
基本上,我想执行以下操作:
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: 但是查询执行LIKE而不是LIKE%...%(类似于PHPMyAdmin),这导致查询仅匹配彼此的完全匹配项,因此类似:
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.
但是我希望这基本上可以匹配,如果row123的全文也位于另一行的值中,然后匹配。
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. 该查询应与
.../file.mp3
, .../file2.mp3
和../dir123
匹配,因为第2行http://link.ext/dir123
也在第1、3和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.