2011-09-26 59 views
1

我想從特定日期的表中獲取電子郵件列表。消除同一個表內的重複項

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

我想消除電子郵件,如果它已經存在之前在表中。所以如果他是在08月之前創建的(由於我爲登錄網站工作,可以多次創建電子郵件)。有人能幫助我嗎?

謝謝

+3

通過「消除」你的意思是「去重複從查詢結果「或」從表中刪除「 –

回答

1

你可以先建立他們的最早created日期獨特的郵件列表,然後從列表中只有那些選擇在一個月中創建的選擇你想要的:

SELECT 
    email 
FROM (
    SELECT 
    email, 
    MIN(created) AS created 
    FROM clients 
    GROUP BY 
    email 
) s 
WHERE MONTH(created)=08 
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 

無論如何,你可以有一年創作的問題...

EDITED:如果你想只得到電子郵件,在月份= 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) 
+0

@ user965644:在嘗試此查詢之前備份您的表! :) – Marco

+0

@ user965644:看看我編輯的帖子 – Marco