我的 MYSQL 数据库中有一个地址表,其结构如下:
- 第一个列 ID 是主要的自动增量列。
- 第二列名称是 varchar。
- 第三列包含由用户填写的地址(文本)。
- 第四列包含地址段,它基本上是小写的地址(第三列),没有任何特殊字符。
- 最后一列包含记录的创建日期。
我希望根据地址/地址段显示所有记录并突出显示可能的重复项。
在本例中,重复项如下:
- 记录 1 和记录 2
- 记录 3 和记录 6
Is there a way to partially match a string in MYSQL or PHP, to achieve the above results?
仅供引用:我已经完成了 SPHINX PHP、SQL 全文搜索等。
我已经苦苦挣扎了两周多,但找不到任何最佳解决方案。
欢迎任何想法、建议、解决方案。
最佳答案
由于 laravel
最初被标记,后来被删除,我认为该策略仍然有帮助。
这是给定的列表:
$lists = [
[
'id' => 1,
'text' => '2693 Edgewood Road Exit',
],
[
'id' => 2,
'text' => '4408 Cost 4657 Avenue',
],
[
'id' => 3,
'text' => '2693 Mapleview Road',
],
[
'id' => 4,
'text' => '4657 Cost Edgewood Avenue',
],
[
'id' => 5,
'text' => '4408 Mapleview Drive Road',
]
];
目标是从每个文本中找到重复/重复的文本。
由于查找一个单词的重复项并不是一个真实的场景,因此我想到用两个单词以及所有可能的组合来查找重复项。
$combinations = [];
foreach ($lists as $list) {
$insideCombo = [];
$insideText = explode(' ', $list['text']);
$length = count($insideText);
for ($i = 0; $i < $length; $i++) {
for ($j = $i + 1; $j < $length; $j++) {
if (isset($insideText[$j])) {
$insideCombo[] = $insideText[$i] . ' ' . $insideText[$j];
}
}
}
$combinations[$list['id']] = $insideCombo;
}
这会回来
// for '2693 Edgewood Road Exit'
1 => array:6 [
0 => "2693 Edgewood"
1 => "2693 Road"
2 => "2693 Exit"
3 => "Edgewood Road"
4 => "Edgewood Exit"
5 => "Road Exit"
]
现在,我们再次循环来比较可能的重复。在这里,我们利用 Laravel 的 Str::containsAll()
$copyCat = [];
foreach ($lists as $list) {
foreach ($combinations as $comboKey => $combination) {
/* no need to compare the text with itself &&
* to avoid duplication of '4 to 2' if '2 to 4' is already mentioned
*/
if ($list['id'] != $comboKey && $list['id'] < $comboKey) {
foreach ($combination as $row) {
if (Str::containsAll($list['text'], explode(' ', $row))) {
$copyCat[] = $list['id'] . ' matches with ' . $comboKey . ' with "' . $row . '"';
}
}
}
}
}
$copyCat
的最终响应
array:5 [
0 => "1 matches with 3 with [2693 Road]"
1 => "2 matches with 4 with [4657 Cost]"
2 => "2 matches with 4 with [4657 Avenue]"
3 => "2 matches with 4 with [Cost Avenue]"
4 => "3 matches with 5 with [Mapleview Road]"
]
请在下面的评论中告诉我。干杯!
关于php - MYSQL PHP : Find duplicates based on Address Column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62808117/