使用带有大列表的“IN”运算符

时间:2014-12-04 22:00:04

标签: mysql

我有一个电子邮件地址列表,我想使用" IN"运营商,以便搜索匹配的电子邮件地址:

SELECT *
FROM Signups
WHERE emailID IN ()

我想在这里放一大堆来自.csv的电子邮件。我该怎么办?

2 个答案:

答案 0 :(得分:1)

如果列表很长,那么我强烈建议您不要使用IN

AFAIK无法读取CSV的值并在查询中使用这些值。 您应该在表中导入数据,对其进行索引,然后将表与您刚刚导入的值连接起来。

示例:

create table tbl_emails(
    id int unisgned not null auto_increment primary key,
    email varchar(100),
    index idx_email(email)
);
-- I'm assuming your file has only one column. If it has more columns, 
-- include them in your table.
load data infile 'path/to/your/file.csv'
into table tbl_emails
fields terminated by ',' optionally enclosed by '"'
lines terminated by '\n'
-- ignore 1 lines -- If your file has a header, uncomment this line
(email) -- If your file has lesser columns than the table, you must include the 
        -- column list here: (field1, field2, ....)
;
select a.*
from signups as a
     inner join tbl_emails as b on a.emailId = b.email;

如果您真的想使用in,那么:

select *
from signups
where emailId in (select email from tbl_emails);

Read the documentation for load data

答案 1 :(得分:0)

如果您只需要一次此查询,则可以在Excel或Notepad ++中打开CSV并运行宏以连接所有地址以及中间的逗号。

对于更具结构性的解决方案,您可以在MySQL中导入CSV,并在in列表中使用该表:

SELECT *
FROM Signups
WHERE emailID IN (SELECT EMAIL from CSVImportTable)