Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将两个字符串与MySQL进行比较_Mysql_Sql_String_String Comparison - Fatal编程技术网

将两个字符串与MySQL进行比较

将两个字符串与MySQL进行比较,mysql,sql,string,string-comparison,Mysql,Sql,String,String Comparison,我不想在一个SQL请求中比较两个字符串,以便检索最佳匹配项,目的是向操作员建议可能的最佳邮政编码。 例如,在法国,我们有整数邮政编码,因此我提出了一个简单的请求: SELECT * FROM myTable ORDER BY abs(zip_code - 75000) 此请求首先返回巴黎最近的数据 不幸的是,英国有类似AB421RS的邮政编码,所以我的请求无法实现。 我在SQL Server中看到一个函数“差异”: 但是我使用MySQL 有没有人能在一个简单的请求中想出一个好主意 注:Leve

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

SELECT *
FROM myTable
ORDER BY abs(zip_code - 75000)
此请求首先返回巴黎最近的数据

不幸的是,英国有类似AB421RS的邮政编码,所以我的请求无法实现。 我在SQL Server中看到一个函数“差异”:

但是我使用MySQL

有没有人能在一个简单的请求中想出一个好主意


注:Levenshtein距离不能做到这一点,因为我真的不想像比较数字一样比较字符串。ABCDEF必须更接近AWXYZ而不是ZBCDEF。

好的,我必须停止提问,然后找到答案

对于社区,我将做以下几点:

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