我有一个名为contacts
的表,其中包含以下字段:
+----+-------+--------------+
| id | name | phone_no |
+----+-------+--------------+
假设我在此表中有以下值:
+----+-------+--------------+
| id | name | phone_no |
+----+-------+--------------+
| 1 | Alex | 9907661234 |--1, 2 are
| 2 | Alex | 09907661234 |--Same contacts but preceding with '0'
| 3 | John | 9879612363 |--Same contacts but preceding with '91'
| 4 | John | 919879612363 |-- 91 is (country code)
| 5 | Shawn | 9979867123 |
+----+-------+--------------+
我想查找包含重复号码的重复联系人数(此处为数字)0
和91
是重复的。
我想要关注输出:
+------------+-------------+
| phone_no | cn |
+------------+-------------+
| 9879612363 | 2 |
| 9907661234 | 2 |
+------------+-------------+
答案 0 :(得分:4)
假设您的电话号码是10个字符(如您在问题中所示),并且可选择以某些代码为前缀。然后你可以在MySQL中使用RIGHT(str,len)
函数来返回指定的最右边的字符数。
查询如下(阅读评论):
SELECT RIGHT(`phone_no`, 10) as `mobile`, -- selecting last 10 digits
count(*) as `tatal_count`
FROM `table_name`
GROUP BY `mobile` -- group by last ten digits
HAVING count(`mobile`) > 1; -- if you want to select on duplicates
工作示例:
创建表格:
CREATE TABLE IF NOT EXISTS `details` (
`id` varchar(64) NOT NULL,
`name` varchar(64) DEFAULT NULL,
`phone` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
)
插入查询:
INSERT INTO `details` VALUES
("1", "Alex", "9907661234"),
("2", "Alex", "09907661234"),
("3", "John", "9879612363"),
("4", "John", "919879612363"),
("5", "Shawn", "9979867123");
[答案]
mysql> SELECT RIGHT(`phone`, 10) as `mobile`,
-> count(*) as `tatal_count`
-> FROM `details`
-> GROUP BY `mobile`
-> ;
+------------+-------------+
| mobile | tatal_count |
+------------+-------------+
| 9879612363 | 2 |
| 9907661234 | 2 |
| 9979867123 | 1 |
+------------+-------------+
3 rows in set (0.04 sec)
假设您只想要那些重复的数字(多个),那么您可以在MySQL中使用HAVING子句:
mysql> SELECT RIGHT(`phone`, 10) as `mobile`,
-> count(*) as `tatal_count`
-> FROM `details`
-> GROUP BY `mobile`
-> HAVING count(`mobile`) > 1;
+------------+-------------+
| mobile | tatal_count |
+------------+-------------+
| 9879612363 | 2 |
| 9907661234 | 2 |
+------------+-------------+
2 rows in set (0.00 sec)
我不会检查代码是否正确,并且假设您在DB中有有效的手机号码
答案 1 :(得分:2)
选择临时表中的数字,删除前导零,或者数字超过指定长度且前导数字与国家/地区代码匹配,将其删除。
然后,您可以在临时表上搜索重复项。
答案 2 :(得分:2)
好吧,我无法测试它,因为我这里没有MySQL服务器。但也许你可以使用以下内容:
SELECT
IF (
phone_no REGEXP '^91' AND LENGTH(phone_no) = 12,
SUBSTRING(phone_no, 3),
IF (
phone_no REGEXP '^0' AND LENGTH(phone_no) = 11,
SUBSTRING(phone_no, 2),
phone_no
)
) AS phoneNumber,
COUNT(*) AS cnt
FROM
`table`
GROUP BY
phoneNumber
用您自己的表名替换'table'。
此查询的作用,如果检查电话号码是否以91或0开头。如果是,则删除前2个或1个字符。获得正确的号码后,我们可以使用'phoneNumber'列进行分组。
编辑:在查询中还添加了“LENGTH”,只是为了确保正常数字没有被剥离。
答案 3 :(得分:2)
select
$phone_number as phone_number, count(phone_number) as cnt
from my_table
where phone_number like concat( '%', $phone_number )
group by concat( '%', $phone_number )
答案 4 :(得分:2)
如果我理解你的问题,这应该是你想要的:
SELECT
CASE
WHEN phone_no LIKE '0%' THEN SUBSTR(phone_no FROM 2)
WHEN phone_no LIKE '91%' THEN SUBSTR(phone_no FROM 3)
ELSE phone_no
END phone_no,
COUNT(*) duplicates
FROM
tablename
GROUP BY
CASE
WHEN phone_no LIKE '0%' THEN SUBSTR(phone_no FROM 2)
WHEN phone_no LIKE '91%' THEN SUBSTR(phone_no FROM 3)
ELSE phone_no
END
HAVING
COUNT(*)>1
请参阅小提琴here。
答案 5 :(得分:2)
我认为你在寻找这个
select if(left(phone_no ,3) = 091 , substring( phone_no,4) ,
if(left(phone_no ,1)= 0, substring( phone_no,2),
if(left(phone_no ,2) = 91 , substring( phone_no,3), phone_no))) as phones, count(*) counts
from contacts
group by phones
having count(*)>1
或者通常选择号码手机的最后10位数字
select RIGHT(`phone_no`, 10) as phones , count(*) counts
from contacts
group by phones
having count(*)>1
输出:
PHONES COUNTS
9879612363 2
9907661234 2
答案 6 :(得分:2)
请按照这个答案。可能会有用。
select phone_no, count(*)
from table_name
group by phone_no
having count(*) > 1