消除同一个表中的重复项

时间:2011-09-26 18:35:06

标签: sql

我希望从表中获取特定日期的电子邮件列表。

Select email FROM clients
WHERE MONTH(created)=08
GROUP BY email

我想删除电子邮件,如果它已经存在于表格之前。所以,如果他是在08月之前创建的。(可以多次创建电子邮件,因为我在登录网站工作)。任何人都可以帮助我。

谢谢

2 个答案:

答案 0 :(得分:1)

您可以先建立一个包含最早created日期的唯一电子邮件列表,然后从该列表中选择仅选择您想要的月份中创建的电子邮件:

SELECT
  email
FROM (
  SELECT
    email,
    MIN(created) AS created
  FROM clients
  GROUP BY
    email
) s
WHERE MONTH(created)=08

答案 1 :(得分:0)

这适用于MySql:

DROP TEMPORARY TABLE IF EXISTS p;
CREATE TEMPORARY table p (email VARCHAR(50));
INSERT INTO p
   SELECT email FROM client 
   WHERE MONTH(created) = 8 
   GROUP BY email;
DELETE FROM client
WHERE email IN 
  (SELECT * FROM p)
AND MONTH(Created) < 8

无论如何,你可能在创作年有问题......

已编辑:如果您只想获得未在月份= 8之前创建的电子邮件,请尝试以下操作:

DROP TEMPORARY TABLE IF EXISTS p;
CREATE TEMPORARY table p (email VARCHAR(50));
INSERT INTO p
   SELECT email FROM client 
   WHERE MONTH(created) < 8 
   GROUP BY email;
SELECT email FROM client
WHERE 
  MONTH(created)=8 AND
  email NOT IN (SELECT * FROM p)