我有两个表'memberships'和'email_list'。每个都包含一个电子邮件地址字段。有些可能在两个表之间重复。我正在尝试制作一个记录列表,但不显示重复项;每封电子邮件只有一次。
认为 DISTINCT 是我需要去的地方,但不确定如何编写查询,或者我是否需要加入。
任何帮助,将不胜感激!
谢谢,瑞克
UNION
查询应该可以解决问题:
SELECT email_address FROM memberships
UNION
SELECT email_address FROM email_list
要选择多个列:
SELECT email_address, first_name, last_name FROM memberships
UNION
SELECT email_address, first_name, last_name FROM email_list
上述查询将返回组合的、不同的 email-firstname-lastname 元组。要消除给定电子邮件地址的重复名字和/或姓氏,请将结果包装在一个GROUP BY
子句中:
SELECT * FROM
(
SELECT email_address, first_name, last_name FROM memberships
UNION
SELECT email_address, first_name, last_name FROM email_list
) AS temp
GROUP BY email_address
进一步以@Salaman 为例
SELECT email_address FROM memberships
UNION
SELECT email_address FROM email_list
然后,您可以通过以下方式获得不同的值:
SELECT DISTINCT email_address FROM
(
SELECT email_address FROM memberships
UNION
SELECT email_address FROM email_list
) as combined_emails
如果您的列表不是很大,那么这应该足以易于使用。