Searching for phone numbers in mysql

后端 未结 16 2320
耶瑟儿~
耶瑟儿~ 2020-12-30 06:40

I have a table which is full of arbitrarily formatted phone numbers, like this

027 123 5644
021 393-5593
(07) 123 456
042123456

I need to sea

相关标签:
16条回答
  • 2020-12-30 07:02

    An out-of-the-box idea, but could you use a "replace" function to strip out any instances of "(", "-" and " ", and then use an "isnumeric" function to test whether the resulting string is a number?

    Then you could do the same to the phone number string you're searching for and compare them as integers.

    Of course, this won't work for numbers like 1800-MATT-ROCKS. :)

    0 讨论(0)
  • 2020-12-30 07:02

    Just an idea, but couldn't you use Regex to quickly strip out the characters and then compare against that like @Matt Hamilton suggested?

    Maybe even set up a view (not sure of mysql on views) that would hold all phone numbers stripped by regex to a plain phone number?

    0 讨论(0)
  • 2020-12-30 07:08

    This looks like a problem from the start. Any kind of searching you do will require a table scan and we all know that's bad.

    How about adding a column with a hash of the current phone numbers after stripping out all formatting characters. Then you can at least index the hash values and avoid a full blown table scan.

    Or is the amount of data small and not expected to grow much? Then maybe just sucking all the numbers into the client and running a search there.

    0 讨论(0)
  • 2020-12-30 07:10

    My solution would be something along the lines of what John Dyer said. I'd add a second column (e.g. phoneStripped) that gets stripped on insert and update. Index this column and search on it (after stripping your search term, of course).

    You could also add a trigger to automatically update the column, although I've not worked with triggers. But like you said, it's really difficult to write the MySQL code to strip the strings, so it's probably easier to just do it in your client code.

    (I know this is late, but I just started looking around here :)

    0 讨论(0)
提交回复
热议问题