表名诗人中有三个字段(_id,name,count)。我想检查表中是否存在诗人名,然后以1为增量更新计数字段,否则插入具有给定名称的新记录,计数为1。这是我的查询。如果在表中找不到名称,它将插入计数的空值。如果我想更新已经存在的记录,虽然效果很好。

insert or replace into poet (_id,Name, count) values (
   (select _id from poet where Name = "SearchName"),
   "SearchName",
    (select count from poet where Name = "SearchName")+ 1)

最佳答案

如何更改查询以使用ifnullcoalesce

insert or replace into poet (_id,Name, count) values (
   (select _id from poet where Name = "SearchName"),
   "SearchName",
    ifnull((select count from poet where Name = "SearchName"), 0) + 1)

10-04 19:34