search

Home  >  Q&A  >  body text

mysql - 在 sqlite 中如何完成“如果某列存在则 select,否则 insert”这样的原子操作?

只知道 replace into 的功能,不知道怎样 select。

大家讲道理大家讲道理2781 days ago639

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 11:36:25

    INSERT OR REPLACE INTO TABLE name (f1, f2, f3) VALUES (v1, v2, v3)

    Well, I read the wrong question. I thought it was insert or update, but it turned out to be select or insert.

    Besides enabling transaction, I didn’t think of any good way to select or insert, but there is a solution to select with default value

    select id, f1, f2 from sometable where id=3 union
    select 999 as id, 'default_value1' as f1, 'default_value2' as f2
    where not exists (select 1 from sometable where id=3)
    

    999 is the default value of id

    reply
    0
  • Cancelreply