我正在尝试从我的所有数据库表中获取我在Web服务器上运行的脚本的电子邮件。不幸的是,某些表中有一些重复的电子邮件。这应该是一个简单的解决方案,使用SELECT DISTINCT,INSERT IGNORE等,但似乎没有任何工作。
这是我目前的查询:
CREATE TEMPORARY TABLE `all_emails` (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
email VARCHAR(80) NOT NULL,
ip_address VARCHAR(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX(email)
);
INSERT IGNORE INTO `all_emails` (email, ip_address)
SELECT email,IP_address
FROM `table_1_emails`
WHERE unsubscribed_at IS NULL;
INSERT IGNORE INTO `all_emails` (email, ip_address)
SELECT email,ip_address
FROM `table_2_emails`
WHERE unsubscribed_at IS NULL;
INSERT IGNORE INTO all_emails (email, ip_address)
SELECT email,ip_address
FROM `table_3_emails`;
INSERT IGNORE INTO `all_emails` (email, ip_address)
SELECT email,ip_address
FROM `table_4_emails`;
SELECT DISTINCT email, ip_address, id FROM `all_emails`;
这仍会产生重复的电子邮件。
答案 0 :(得分:0)
只需使用UNION
即可删除单个INSERT
中的重复项:
INSERT IGNORE INTO `all_emails` (email, ip_address)
SELECT email,IP_address
FROM `table_1_emails`
WHERE unsubscribed_at IS NULL
UNION
SELECT email,ip_address
FROM `table_2_emails`
WHERE unsubscribed_at IS NULL
UNION
SELECT email,ip_address
FROM `table_3_emails`
UNION
SELECT email,ip_address
FROM `table_4_emails`;