PostgreSQL 满足条件时插入数据

例如:当表中不存在某记录时,才插入这条记录。

INSERT INTO 表名(列名1, 列名2)
SELECT
    '值1', '值2'
WHERE NOT EXISTS (
    SELECT * FROM 表名 WHERE 列名1 = '值1', 列名2 = '值2'
);

参考

  1. 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';
    
  2. database - Conditional INSERT INTO statement in postgres - Stack Overflow

posted @ 2017-11-23 22:26  hiwangzi  阅读(3390)  评论(0编辑  收藏  举报