Mysql“不在”选择问题

时间:2011-10-27 15:08:42

标签: 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行

有什么想法吗?或者另一种进行此类查询的方式?

尼古拉斯

5 个答案:

答案 0 :(得分:1)

您可以在电子邮件字段中添加索引以加快查询速度并在子查询中选择“DISTINCT”电子邮件以减少搜索值:

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)

答案 1 :(得分: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)

答案 2 :(得分: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() 

答案 3 :(得分: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 */

答案 4 :(得分: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)