[英]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.