Searching for phone numbers in mysql

后端 未结 16 2319
耶瑟儿~
耶瑟儿~ 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 06:52

    if this is something that is going to happen on a regular basis perhaps modifying the data to be all one format and then setup the search form to strip out any non-alphanumeric (if you allow numbers like 310-BELL) would be a good idea. Having data in an easily searched format is half the battle.

    0 讨论(0)
  • 2020-12-30 06:53

    As John Dyer said, you should consider fixing the data in the DB and store only numbers. However, if you are facing the same situation as mine (I cannot run a update query) the workaround I found was combining 2 queries.

    The "inside" query will retrieve all the phone numbers and format them removing the non-numeric characters.

    SELECT REGEXP_REPLACE(column_name, '[^0-9]', '') phone_formatted FROM table_name
    

    The result of it will be all phone numbers without any special character. After that the "outside" query just need to get the entry you are looking for. The 2 queries will be:

    SELECT phone_formatted FROM (
        SELECT REGEXP_REPLACE(column_name, '[^0-9]', '') phone_formatted FROM table_name
    ) AS result WHERE phone_formatted = 9999999999
    

    Important: the AS result is not used but it should be there to avoid erros.

    0 讨论(0)
  • 2020-12-30 06:54

    See

    http://www.mfs-erp.org/community/blog/find-phone-number-in-database-format-independent

    It is not really an issue that the regular expression would become visually appalling, since only mysql "sees" it. Note that instead of '+' (cfr. post with [\D] from the OP) you should use '*' in the regular expression.

    Some users are concerned about performance (non-indexed search), but in a table with 100000 customers, this query, when issued from a user interface returns immediately, without noticeable delay.

    0 讨论(0)
  • 2020-12-30 06:55

    Is it possible to run a query to reformat the data to match a desired format and then just run a simple query? That way even if the initial reformatting is slow you it doesn't really matter.

    0 讨论(0)
  • 2020-12-30 06:59

    i suggest to use php functions, and not mysql patterns, so you will have some code like this:

    $tmp_phone = '';
    for ($i=0; $i < strlen($phone); $i++)
       if (is_numeric($phone[$i]))
           $tmp_phone .= '%'.$phone[$i];
    $tmp_phone .= '%';
    $search_condition .= " and phone LIKE '" . $tmp_phone . "' ";
    
    0 讨论(0)
  • 2020-12-30 07:00

    a possible solution can be found at http: //udf-regexp.php-baustelle.de/trac/

    additional package need to be installed, then you can play with REGEXP_REPLACE

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