2017-03-03 57 views
1

在mySql中如果没有行存在特定电子邮件,我如何插入一行,我需要从数据库表中创建一个缺少的行,I有一个五千个电子邮件的文件,我不知道哪个电子邮件已经在表中,哪些不是。如何在MySQL中插入行如果行不存在相同的值

我试图构建这样每行一个SQL语句,但它是无效的SQL语法

insert into License (email) select unique '[email protected]' where not exists (select 1 from License where email='[email protected]'); 

电子邮件是不是表的主键,而不是neccessarily独特的,所有我关心的是,如果没有记录的电子邮件地址添加记录,否则不。

回答

2

您可以通过具有用于where一个from条款解决这个问题:

insert into License (email) 
    select email 
    from (select '[email protected]' as email) t 
    where not exists (select 1 from License l where l.email = t.email); 

然而,它更有意义做所有的刀片在一个声明:

insert into License (email) 
    select t.email 
    from database_table t 
    where not exists (select 1 from License l where l.email = t.email); 

可以添加limit 1000,如果你只想要1000个。

+0

谢谢你的工作 –

相关问题