這是可能的,但凌亂。如果您可以將「區域」字段標準化爲單個列,這將有所幫助 - 這將使解決方案更加簡潔快速!
示例實現可能是:
select area, 1 as match_quality
from property
where area = '1-0-3-4'
union
select area, 2 as match_quality
from property
where area like '1-0-3-_'
union
select area, 3 as match_quality
from property
where area like '1-0-_-4'
這假定區域之間的距離爲任何在給定的列中的值是相同的。如果情況並非如此,可以通過檢索代碼(通過SUBSTRING)並根據需要進行任何算術運算來優化它。
如果將區域轉換爲單個列,這會變得更加容易,更好,也更快(上面的聯合中的最後一個查詢會很慢,因爲它無法有效地使用索引)。
例如:
select *, 1 as match_quality
from property
where area1 = 1
and area2 = 0
and area3 = 3
and area4 = 4
union
select *, 2 as match_quality
from property
where area 1 = 1
and area 2 = 0
and area 4 = 4
union
select *, 3 as match_quality
from property
where area 1 = 1
and area2 = 0
and area4 = 4
我認爲你需要使用一個匹配算法[** Levenshtein距離算法**](http://en.wikipedia.org/wiki/Levenshtein_distance)爲例。 –