“Insert if not exists” statement in SQLite

后端 未结 4 961
借酒劲吻你
借酒劲吻你 2020-11-22 12:14

I have an SQLite database. I am trying to insert values (users_id, lessoninfo_id) in table bookmarks, only if both do not exist before

4条回答
  •  Happy的楠姐
    2020-11-22 12:45

    insert into bookmarks (users_id, lessoninfo_id)
    
    select 1, 167
    EXCEPT
    select user_id, lessoninfo_id
    from bookmarks
    where user_id=1
    and lessoninfo_id=167;
    

    This is the fastest way.

    For some other SQL engines, you can use a Dummy table containing 1 record. e.g:

    select 1, 167 from ONE_RECORD_DUMMY_TABLE
    

提交回复
热议问题