2017-03-03 54 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

謝謝你的工作 –

相關問題