Mysql“不在”选择问题

发布于 2024-12-12 00:28:21 字数 428 浏览 0 评论 0原文

我只需要在一张表中找到今天参加比赛的所有人员,而不是前几天的获胜者...

我的查询是:

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 行

有什么想法吗?或者用另一种方式进行这种查询?

尼古拉斯

I need to find in one table only , all persons having played to a contest today and not be winners from previous days...

My query is :

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)

There is something wrong...mysql is not responding when I try to execute my query (about 7000 rows in the table)

Any idea ? Or another way to make this kind of query ?

Nicolas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木槿暧夏七纪年 2024-12-19 00:28:21

您可以在电子邮件字段上添加索引以加快查询速度,并在子查询中选择“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)

You can add an index on the email field to speed up the query and select "DISTINCT" email in subquery to reduce searched values:

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)
情仇皆在手 2024-12-19 00:28:21

尝试一下(与你的略有不同):

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)

Try with this (slightly different from yours):

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)
恋竹姑娘 2024-12-19 00:28:21
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 */
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 */
泛滥成性 2024-12-19 00:28:21

这不是等价的吗:

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() 

Wouldn't this be the equivalent:

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() 
此生挚爱伱 2024-12-19 00:28:21
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)
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文