例如:当表中不存在某记录时,才插入这条记录。
INSERT INTO 表名(列名1, 列名2)
SELECT
'值1', '值2'
WHERE NOT EXISTS (
SELECT * FROM 表名 WHERE 列名1 = '值1', 列名2 = '值2'
);
参考
PostgreSQL: Documentation: 10: INSERT
This example inserts some rows into table films from a table tmp_films with the same column layout as films:
INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07';
database – Conditional INSERT INTO statement in postgres – Stack Overflow