PostgreSQL 知足條件時插入數據

例如:當表中不存在某記錄時,才插入這條記錄。html

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

參考

  1. PostgreSQL: Documentation: 10: INSERTsql

    This example inserts some rows into table films from a table tmp_films with the same column layout as films:post

    INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07';
  2. database - Conditional INSERT INTO statement in postgres - Stack Overflowpostgresql

相關文章
相關標籤/搜索