在mySql中,如果某封电子邮件不存在行,我如何插入行,我需要从数据库表中创建一千行,我有一个包含5000封电子邮件的文件,我不知道哪一封电子邮件已经在表中了,而哪一封没有。
我试图为每一行构造一个sql语句,但它是无效的sql语法。
insert into License (email) select unique 'person@gmail.com' where not exists (select 1 from License where email='person@gmail.com');电子邮件不是表的主键,不一定是唯一的,我所关心的是如果没有记录与电子邮件地址添加一个记录,否则不要。
发布于 2017-03-03 13:38:46
您可以通过为该from设置一个where子句来修复这个问题。
insert into License (email)
    select email
    from (select 'person@gmail.com' 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个。
https://stackoverflow.com/questions/42580374
复制相似问题