2011-10-27 68 views
1

我需要在一個表中只,所有的人今天已經發揮到了一個競賽,找到並不會從以前的獲獎者天...Mysql的「不是」選擇問題

我的查詢是:

SELECT Email,FName,Name,ID 
FROM `voucher_registrations` 
WHERE DATE_FORMAT(`Date_Submitted`,'%Y-%c-%d') = curdate() 
and Email not in (select email 
        from `voucher_registrations` 
        where winner=1) 

也有一些是錯誤的... MySQL是不響應當我嘗試執行我的查詢

任何想法(表中的約7000行)?或另一種方式來做這種查詢?

薩科

回答

1

可以上的電子郵件字段添加一個索引加快查詢並選擇「獨特的」子查詢,以減少電子郵件搜索值:

SELECT Email,FName,Name,ID 
FROM `voucher_registrations` 
WHERE Date_Submitted = CURDATE() 
    AND Email not in 
    (SELECT DISTINCT Email FROM`voucher_registrations` WHERE winner=1) 
+0

感謝了很多,得到了它與電子郵件作爲山坳工作指標。 – user1016753

1

這種嘗試(略從不同的你):

SELECT Email,FName,Name,ID 
FROM `voucher_registrations` 
WHERE DATE_FORMAT(`Date_Submitted`,'%Y-%c-%d') = curdate() 
    AND Email NOT IN 
    (SELECT DISTINCT email FROM `voucher_registrations` 
    WHERE winner = 1) 
+0

不會。這隻會在今天的日期過濾贏家。他希望從任何日期篩選出優勝者。 –

+0

@JoeStefanelli:對,編輯我的答案。謝謝;) – Marco

0

這是不是可以等價的:

SELECT v.Email,v.FName,v.Name,v.ID 
FROM `voucher_registrations` v 
JOIN (select email from `voucher_registrations` where winner <>1)t on t.email=v.email 
WHERE DATE_FORMAT(`v.Date_Submitted`,'%Y-%c-%d') = curdate() 
+0

美好:同一時間相同的答案;) – Marco

+0

沒有。這隻會在今天的日期篩選獲勝者。他希望從任何日期篩選出優勝者。 –

+0

Gotcha,更新爲使用連接,做一個「in」總是更快然後是「不在」 – brendan

0
SELECT vr1.Email,vr1.FName,vr1.Name,vr1.ID 
    FROM `voucher_registrations` vr1 
     LEFT JOIN `voucher_registrations` vr2 
      ON vr1.Email = vr2.Email 
       AND vr2.winner = 1 
    WHERE DATE_FORMAT(vr1.`Date_Submitted`,'%Y-%c-%d') = curdate() 
     AND vr2.Email IS NULL /* Email was not found among winners */ 
0
SELECT Email,FName,Name,ID 
FROM `voucher_registrations` 
WHERE DATE_FORMAT(`Date_Submitted`,'%Y-%c-%d') = curdate() 
and Email <> (select email 
       from `voucher_registrations` 
       where winner=1)