SQLite - UPSERT *not* INSERT or REPLACE

后端 未结 18 2915
猫巷女王i
猫巷女王i 2020-11-21 23:53

http://en.wikipedia.org/wiki/Upsert

Insert Update stored proc on SQL Server

Is there some clever way to do this in SQLite that I have not thought of?

18条回答
  •  不要未来只要你来
    2020-11-22 00:18

    Expanding on Aristotle’s answer you can SELECT from a dummy 'singleton' table (a table of your own creation with a single row). This avoids some duplication.

    I've also kept the example portable across MySQL and SQLite and used a 'date_added' column as an example of how you could set a column only the first time.

     REPLACE INTO page (
       id,
       name,
       title,
       content,
       author,
       date_added)
     SELECT
       old.id,
       "about",
       "About this site",
       old.content,
       42,
       IFNULL(old.date_added,"21/05/2013")
     FROM singleton
     LEFT JOIN page AS old ON old.name = "about";
    

提交回复
热议问题