MySQL在記錄不存在時的插入:
範例:插入多筆記錄
假設有一個主鍵為client_id 的clients 表,可以使用下面的語句:
INSERT INTO clients
(client_id, client_name, client_type)
SELECT supplier_id, supplier_name, 'advertpplising'
FROMent_type
)suppliers
WHERE not exists (select * from clients
where clients.client_id = suppliers.supplier_id);
範例:插入單一記錄
INSERT INTO clients
(client_id, client_name, client_type)
SELECT 10345, 'IBM', 'advertising'
FROM dual
WHERE not exists (select * from clients
where
clients.直接跟上要插入欄位的值,即使這些值還不存在目前表中。