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

    原文作者:PostgreSQL
    原文地址: https://www.cnblogs.com/hiwangzi/p/7885540.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞