这是可能的,但很混乱。如果您可以将“区域”字段标准化为单独的列,这将有所帮助 - 这将使解决方案更整洁、更快!
一个示例实现可能是:
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)并对其进行任何您需要的算术来改进它。
如果将 area 转换为单个列,这将变得更容易、更好、更快(上述联合中的最后一个查询会很慢,因为它不能有效地使用索引)。
例如:
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