SQLite 实现if not exist 类似功能的操作


发布者 蔚蓝天空  发布时间 1619406303876
关键字 心得体会 

  SQLite 实现if not exist 类似功能的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。

  需要实现:

  if not exists(select * from ErrorConfig where Type='RetryWaitSeconds')

  begin

  insert into ErrorConfig(Type,Value1)

  values('RetryWaitSeconds','3')

  end

  只能用:

  insert into ErrorConfig(Type,Value1)

  select 'RetryWaitSeconds','3'

  where not exists(select * from ErrorConfig where Type='RetryWaitSeconds')

  因为 SQLite 中不支持SP

  补充:sqlite3中NOT IN 不好用的问题

  在用sqlite3熟悉SQL的时候遇到了一个百思不得其解的问题,也没有在google上找到答案。虽然最后用“迂回”的方式碰巧解决了这个问题,但暂时不清楚原理是什么,目前精力有限,所以暂时记录下来,有待继续研究。

  数据库是这样的:

  CREATE TABLE book (

  id integer primary key,

  title text,

  unique(title)

  );

  CREATE TABLE checkout_item (

  member_id integer,

  book_id integer,

  movie_id integer,

  unique(member_id, book_id, movie_id) on conflict replace,

  unique(book_id),

  unique(movie_id)

  );

  CREATE TABLE member (

  id integer primary key,

  name text,

  unique(name)

  );

  CREATE TABLE movie (

  id integer primary key,

  title text,

  unique(title)

  );

  该数据库包含了4个表:book, movie, member, checkout_item。其中,checkout_item用于保存member对book和movie的借阅记录,属于关系表。

  问一:哪些member还没有借阅记录?

  SQL语句(SQL1)如下:

  1SELECT * FROM member WHERE id NOT IN(SELECT member_id FROM checkout_item);

  得到了想要的结果。

  问二:哪些book没有被借出?

  这看起来与上一个是类似的,于是我理所当然地运行了如下的SQL语句(SQL2):

  1SELECT * FROM book WHERE id NOT IN(SELECT book_id FROM checkout_item);

  可是——运行结果没有找到任何记录! 我看不出SQL2与SQL1这两条语句有什么差别,难道是book表的问题?于是把NOT去掉,运行了如下查询语句:

  1SELECT * FROM book WHERE id IN(SELECT book_id FROM checkout_item);

  正确返回了被借出的book,其数量小于book表里的总行数,也就是说确实是有book没有借出的。

  接着google(此处省略没有营养的字),没找到解决方案。可是,为什么member可以,book就不可以呢?它们之前有什么不同?仔细观察,发现checkout_item里的book_id和movie_id都加了一个unique,而member_id则没有。也许是这个原因?不用id了,换title试试:

  SELECT * FROM book WHERE

  title NOT IN(

  SELECT title FROM book WHERE id IN(

  SELECT book_id FROM checkout_item));

  确实很迂回,但至少work了。。。

  问题原因:当NOT碰上NULL

  事实是,我自己的解决方案只不过是碰巧work,这个问题产生跟unique没有关系。邱俊涛的解释是,“SELECT book_id FROM checkout_item”的结果中含有null值,导致NOT也返回null。当一个member只借了movie而没有借book时,产生的checkout_item中book_id就是空的。

  解决方案是,在选择checkout_item里的book_id时,把值为null的book_id去掉:

  1SELECT * FROM book WHERE id NOT IN(SELECT book_id FROM checkout_item WHERE book_id IS NOT NULL);

  总结

  我在解决这个问题的时候方向是不对的,应该像调试程序一样,去检查中间结果。比如,运行如下语句,结果会包含空行:

  1SELECT book_id FROM checkout_item

  而运行下列语句,结果不会包含空行:

  1SELECT member_id FROM checkout_item

  这才是SQL1与SQL2两条语句执行过程中的差别。根据这个差别去google,更容易找到答案。当然了,没有NULL概念也是我“百思不得其解”的原因。

dribbble.com/baecu/collections/4664951
dribbble.com/baecu/collections/4664955
dribbble.com/baecu/collections/4664956
dribbble.com/baecu/collections/4664958
dribbble.com/baecu/collections/4664959
dribbble.com/baecu/collections/4664964
dribbble.com/baecu/collections/4664967
dribbble.com/baecu/collections/4664970
dribbble.com/baecu/collections/4664974
dribbble.com/baecu/collections/4664977
dribbble.com/keaie/collections/4665018
dribbble.com/keaie/collections/4665015
dribbble.com/keaie/collections/4665013
dribbble.com/keaie/collections/4665012
dribbble.com/keaie/collections/4665008
dribbble.com/keaie/collections/4665002
dribbble.com/keaie/collections/4665001
dribbble.com/keaie/collections/4665000
dribbble.com/keaie/collections/4664998
dribbble.com/keaie/collections/4664996
dribbble.com/xiodc/collections/4665055
dribbble.com/xiodc/collections/4665052
dribbble.com/xiodc/collections/4665050
dribbble.com/xiodc/collections/4665047
dribbble.com/xiodc/collections/4665045
dribbble.com/xiodc/collections/4665043
dribbble.com/xiodc/collections/4665042
dribbble.com/xiodc/collections/4665038
dribbble.com/xiodc/collections/4665036
dribbble.com/xiodc/collections/4665033
dribbble.com/haiuo/collections/4665076
dribbble.com/haiuo/collections/4665075
dribbble.com/haiuo/collections/4665072
dribbble.com/haiuo/collections/4665070
dribbble.com/haiuo/collections/4665068
dribbble.com/haiuo/collections/4665067
dribbble.com/haiuo/collections/4665064
dribbble.com/haiuo/collections/4665063
dribbble.com/haiuo/collections/4665062
dribbble.com/haiuo/collections/4665061
dribbble.com/ponch/collections/4665108
dribbble.com/ponch/collections/4665107
dribbble.com/ponch/collections/4665106
dribbble.com/ponch/collections/4665104
dribbble.com/ponch/collections/4665102
dribbble.com/ponch/collections/4665100
dribbble.com/ponch/collections/4665099
dribbble.com/ponch/collections/4665098
dribbble.com/ponch/collections/4665097
dribbble.com/ponch/collections/4665094
dribbble.com/baieio/collections/4668257
dribbble.com/baieio/collections/4668252
dribbble.com/baieio/collections/4668247
dribbble.com/baieio/collections/4668244
dribbble.com/baieio/collections/4668239
dribbble.com/baieio/collections/4668233
dribbble.com/baieio/collections/4668230
dribbble.com/baieio/collections/4668215
dribbble.com/baieio/collections/4668202
dribbble.com/baieio/collections/4668191
dribbble.com/niueda/collections/4668398
dribbble.com/niueda/collections/4668387
dribbble.com/niueda/collections/4668377
dribbble.com/niueda/collections/4668362
dribbble.com/niueda/collections/4668347
dribbble.com/niueda/collections/4668333
dribbble.com/niueda/collections/4668320
dribbble.com/niueda/collections/4668308
dribbble.com/niueda/collections/4668305
dribbble.com/niueda/collections/4668299
dribbble.com/nckel/collections/4668793
dribbble.com/nckel/collections/4668782
dribbble.com/nckel/collections/4668757
dribbble.com/nckel/collections/4668747
dribbble.com/nckel/collections/4668732
dribbble.com/nckel/collections/4668718
dribbble.com/nckel/collections/4668704
dribbble.com/nckel/collections/4668687
dribbble.com/nckel/collections/4668670
dribbble.com/nckel/collections/4668659
dribbble.com/bcaie/collections/4669034
dribbble.com/bcaie/collections/4669017
dribbble.com/bcaie/collections/4669001
dribbble.com/bcaie/collections/4668988
dribbble.com/bcaie/collections/4668977
dribbble.com/bcaie/collections/4668961
dribbble.com/bcaie/collections/4668951
dribbble.com/bcaie/collections/4668938
dribbble.com/bcaie/collections/4668923
dribbble.com/bcaie/collections/4668909
dribbble.com/wheca/collections/4669282
dribbble.com/wheca/collections/4669271
dribbble.com/wheca/collections/4669261
dribbble.com/wheca/collections/4669248
dribbble.com/wheca/collections/4669235
dribbble.com/wheca/collections/4669219
dribbble.com/wheca/collections/4669206
dribbble.com/wheca/collections/4669191
dribbble.com/wheca/collections/4669179
dribbble.com/wheca/collections/4669165
dribbble.com/jknfda/collections/4673021
dribbble.com/jknfda/collections/4673019
dribbble.com/jknfda/collections/4673017
dribbble.com/jknfda/collections/4673016
dribbble.com/jknfda/collections/4673015
dribbble.com/jknfda/collections/4673013
dribbble.com/jknfda/collections/4673012
dribbble.com/jknfda/collections/4673011
dribbble.com/jknfda/collections/4673010
dribbble.com/jknfda/collections/4673008
dribbble.com/jkiea/collections/4673041
dribbble.com/jkiea/collections/4673040
dribbble.com/jkiea/collections/4673038









  开源的 OurJS
OurJS开源博客已经迁移到 OnceOA 平台。

  关注我们
扫一扫即可关注我们:
OnceJS

OnceOA