0

我不想比较 SQL 请求中的两个字符串,以便检索最佳匹配,目的是向操作员建议可能的最佳邮政编码。例如,在法国,我们有整数邮政编码,所以我提出了一个简单的请求:

SELECT *
FROM myTable
ORDER BY abs(zip_code - 75000)

该请求首先返回最接近巴黎的数据。

不幸的是,英国有像 AB421RS 这样的邮政编码,所以我的请求无法做到。我在 SQL Server 中看到一个函数“差异”:http ://www.java2s.com/Code/SQLServer/String-Functions/DIFFERENCEworkoutwhenonestringsoundssimilartoanotherstring.htm

但我使用 MySQL ..

有没有人有一个好主意可以在一个简单的请求中完成这个技巧?

PS:Levenshtein Distance 不会这样做,因为我真的不想像它们是数字那样比较字符串。ABCDEF 必须更接近 AWXYZ 而不是 ZBCDEF。

4

1 回答 1

2

好的,我必须停止提问并在之后找到答案!

对于社区,这是我将要做的:

select *
from myTable
order by abs(ascii(substring(zip_code,1,1)) - ascii(substring('AAAAA',1,1))) asc,
         abs(ascii(substring(zip_code,2,1)) - ascii(substring('AAAAA',2,1))) asc,
         abs(ascii(substring(zip_code,3,1)) - ascii(substring('AAAAA',3,1))) asc,
         abs(ascii(substring(zip_code,4,1)) - ascii(substring('AAAAA',4,1))) asc,
         abs(ascii(substring(zip_code,5,1)) - ascii(substring('AAAAA',5,1))) asc
于 2010-06-18T15:50:22.017 回答