Insert, on duplicate update in PostgreSQL?

前端 未结 16 2641
别那么骄傲
别那么骄傲 2020-11-21 04:52

Several months ago I learned from an answer on Stack Overflow how to perform multiple updates at once in MySQL using the following syntax:

INSERT INTO table          


        
16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 05:44

    Similar to most-liked answer, but works slightly faster:

    WITH upsert AS (UPDATE spider_count SET tally=1 WHERE date='today' RETURNING *)
    INSERT INTO spider_count (spider, tally) SELECT 'Googlebot', 1 WHERE NOT EXISTS (SELECT * FROM upsert)
    

    (source: http://www.the-art-of-web.com/sql/upsert/)

提交回复
热议问题