如何在以下查询中找到重复的电子邮件?
SELECT b_email AS email FROM user WHERE status = 3 AND b_email != ''
UNION (SELECT c_email AS email FROM user WHERE status = 3 AND c_email != '')
UNION (SELECT user_email AS email FROM user WHERE status = 3 AND user_email != '')
UNION (SELECT user_email AS email FROM sub_user WHERE status = 3 AND user_email != '')
UNION (SELECT su_email AS email FROM sub_user WHERE status = 3 AND su_email != '')
ORDER BY email ASC
感谢。
答案 0 :(得分:2)
您的查询结果不会重复,因为UNION
会删除重复项。要保留重复项,请改为使用UNION ALL
:
SELECT b_email AS email FROM user WHERE status = 3 AND b_email != ''
UNION ALL SELECT c_email AS email FROM user WHERE status = 3 AND c_email != ''
...
要确定哪些电子邮件地址重复,您可以执行以下操作:
UNION ALL
代替UNION
)以下是如何操作(注意这假设您发布的查询有效):
SELECT email, COUNT(*)
FROM (
SELECT b_email AS email FROM user WHERE status = 3 AND b_email != ''
UNION ALL SELECT c_email AS email FROM user WHERE status = 3 AND c_email != ''
UNION ALL SELECT user_email AS email FROM user WHERE status = 3 AND user_email != ''
UNION ALL SELECT user_email AS email FROM sub_user WHERE status = 3 AND user_email != ''
UNION ALL SELECT su_email AS email FROM sub_user WHERE status = 3 AND su_email != ''
) EmailGlom
GROUP BY email
HAVING COUNT(*) > 1
您的结果将是在内部查询中多次显示的每个电子邮件地址。