Mysql-5

插入忽略條件

  • May 6, 2013

我需要插入一些可能有重複的記錄。要覆蓋/跳過的列是mytable.email並且mytable.fooFlag應該是 1。

我想出了這樣的事情:

INSERT IGNORE INTO mytable  
VALUES ('somemailaddress')  
ONLY IF 'somemailaddress' DOES NOT EXIST  
AND fooFlag = 1

如果email是唯一的,那就是“插入忽略”的用途!

如果email是 UNIQUE 並且您想在其他欄位已經在表中時“更新”它,那麼這就是“INSERT … ON DUPLICATE KEY UPDATE”的用途。

INSERT  INTO mytable (email)
SELECT sourcetable.email FROM sourcetable
WHERE sourcetable.email NOT IN (SELECT mytable.email from mytable where mytable.fooflag=1)

引用自:https://dba.stackexchange.com/questions/28211