我想选择使用多个电话号码的人。如果电话号码匹配大于1,如何查询结果?
我在下面尝试过此查询。
SELECT DISTINCT
group,
email,
phone
FROM
account
WHERE
group = 6
GROUP BY
phone
HAVING
COUNT( phone ) > 1
ORDER BY
phone
group email phone
6 xxsd@adsf.xsd 003434234523
6 wet@ardsf.xsd 003434234523
6 rwwe@adsf.xsd 003364534579
6 hdgh@adsf.xsd 003434234523
因此,如果电话号码(003434234523)大于1,则将执行查询。
答案 0 :(得分:0)
使用EXISTS查找使用同一手机的用户的电子邮件:
SELECT
a.group,
a.email,
a.phone
FROM
account a
WHERE
a.group = 6
AND EXISTS (
SELECT 1 FROM account
WHERE group = 6 AND phone = a.phone AND email <> a.email
)
ORDER BY a.phone
我使用WHERE group = 6
是因为您在代码中包含了它。
答案 1 :(得分:0)
这将在第6组的帐户表中找到所有使用了一个以上帐户使用的电话号码的记录。
SELECT *
FROM account
WHERE `group` = 6
AND phone IN (SELECT phone
FROM account
WHERE `group` = 6
GROUP BY phone
HAVING COUNT(*) > 1)
ORDER BY phone;
答案 2 :(得分:0)
多个用户正在使用的相同电话号码。
重复的电话:
SELECT phone, count(*)
FROM account
WHERE group = 6
GROUP BY phone
HAVING COUNT(*) > 1
使用重复的电话发送电子邮件:
SELECT group, email, phone
FROM account
WHERE phone IN (
SELECT phone, count(*)
FROM account
WHERE group = 6
GROUP BY phone
HAVING COUNT(*) > 1
)
ORDER BY phone