How to find a record which best matches to given string using only mySQL database?

问题: I am having a hard time figuring this one out so I came here for help. I have a table x_25_operators which has a field called mask. Masks are numbers with length between...

问题:

I am having a hard time figuring this one out so I came here for help.

I have a table x_25_operators which has a field called mask. Masks are numbers with length between 7-11 digits. What I am trying to achieve here is to find the best match for my query at this particular table.

Given the scenario:

x_25_operators

| some_other_data | mask
         .          486737
         .          616724
         .          915776

I have a number: 48915776148 (this number is fixed-length, always 11 digits)

I am looking for a query which will return a row containing mask 915776 (or all rows fitting to this searched number as filtering best-matching-one out should be a piece of cake).

I was thinking of using LIKE as the filter but such query:

SELECT * FROM x_25_operators WHERE mask LIKE '48915776148'

returns an empty query (which should be kind of obvious).

I am using a mySQL database.

Any ideas how to tackle such problem? I am open to any suggestions.


回答1:

You can use LOCATE()

SELECT * FROM x_25_operators WHERE LOCATE(mask , "48915776148")

Here is some more info: https://www.w3resource.com/mysql/string-functions/mysql-locate-function.php

  • 发表于 2019-01-18 21:06
  • 阅读 ( 211 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除