简体   繁体   English

Select 其中 field1 或 field2 或 field3 等于电话号码

[英]Select where field1 or field2 or field3 is equal to a phone number

I am looking at a table with phone1, phone2 and mobile.我正在看一张带有 phone1、phone2 和 mobile 的桌子。 However the phone strings are not formatted properly.但是,电话字符串的格式不正确。

The phone numbers are basically free text like this:电话号码基本上是这样的自由文本:

row1: phone1:19123123123
row2: mobile:+1 912 123 123
row3: phone2:1 912 123 123
row4: mobile:(+1) 912 123 123
row5: phone2:(+1)912-123-123

Is it possible to write a SQL query to find if one of the fields phone1, phone2 or mobile is +19123123123 but it needs to be able to match any one of those free text examples?是否可以编写一个 SQL 查询来查找 phone1、phone2 或 mobile 字段之一是否为 +19123123123,但它需要能够匹配这些自由文本示例中的任何一个? Thanks谢谢

So in the above example, it should return all 5 records be those free text match that phone number and is in one of the 3 fields.因此,在上面的示例中,它应该返回所有 5 条记录,即那些与该电话号码匹配的自由文本,并且位于 3 个字段之一中。

Assuming the only numbers and + in the column are in the phone number, you can use:假设列中唯一的数字和+在电话号码中,您可以使用:

where regexp_replace(col, '[^+0-9]', '') = '+19123123123'

This removes everything that is not a + or digit.这将删除不是+或数字的所有内容。

you can use Regex to identify phone number pattern or you can just remove all parenthesis and spaces from the both side of the where clause and then compare.您可以使用正则表达式来识别电话号码模式,或者您可以从 where 子句的两侧删除所有括号和空格,然后进行比较。

     SELECT * FROM T 
    WHERE REPLACE(REPLACE(REPLACE(phone1, '(',''),')',''),' ','') = '+19123123123'
        OR REPLACE(REPLACE(REPLACE(phone1, '(',''),')',''),' ','') = '19123123123'
        OR REPLACE(REPLACE(REPLACE(phone2, '(',''),')',''),' ','') = '+19123123123'
        OR REPLACE(REPLACE(REPLACE(phone2, '(',''),')',''),' ','') = '19123123123'
        OR REPLACE(REPLACE(REPLACE(mobile, '(',''),')',''),' ','') = '+19123123123'
        OR REPLACE(REPLACE(REPLACE(mobile, '(',''),')',''),' ','') = '19123123123'

As suggested by Juergen, you can strip all non numeric character and compare.正如 Juergen 所建议的,您可以去除所有非数字字符并进行比较。 If you are on MySQL 8.0+, you can use below code如果您使用的是 MySQL 8.0+,您可以使用以下代码

REGEXP_REPLACE(SUBSTRING_INDEX(column_name, ':', -1),'[^0-9]+',"")

Assumption: Column value is always being separated by colon(:) after mobile/phone/sometext.假设:在 mobile/phone/sometext 之后,列值始终用冒号 (:)分隔。 Here it will take the second part of your string after colon and then strip the non numeric characters在这里,它将在冒号之后获取字符串的第二部分,然后去除非数字字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mysql在哪里查询…选择* where field1 + field2 + field3 &lt;=字段4 - Mysql where query… select * where field1 +field2 + field3 <= field 4 MYSQL仅在field1或field2不等于文本的情况下选择field1,field2 - MYSQL select field1, field2 only where field1 or field2 does not equal text 其中field1 + field2 + field3就像“%my_own_string%”? - Where field1 + field2 + field3 LIKE “%my_own_string%”? mysql相关子查询:选择field1,其中max(field2) - mysql correlated subquery : select field1 where max(field2) MySQL-选择field1和field2都大于0的地方 - MySQL - Select where both field1 and field2 are greater than 0 在行(val11,val21,val31),…,(val11,val21,val31)中选择其中(field1,field2,field3)的行 - Select rows where (field1, field2, field3) in set (val11,val21,val31),…,(val11,val21,val31) CodeIgniter其中值IN(Field1,Field2) - CodeIgniter Where value IN (Field1,Field2) MySQL IF函数-WHERE&#39;field1&gt; 0&#39;吗? &#39;field2 &lt;field1&#39;:&#39;&#39; - MySQL IF function - WHERE 'field1 > 0' ? 'field2 < field1' : '' 从select1字段1插入field1,field2,field2仅填充1个字段 - Inserting into field1,field2 from a select field1, field2 populates only 1 field 通过Field1 Field2 Field3 ASC和DESC对查询结果进行排序 - Sort query results by Field1 Field2 Field3 ASC and DESC so
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM