如果存在,则在行上增加字段,否则插入新行

Increment field on row if exists, else insert new row

想象一个 table 存储问题的标签,例如堆栈溢出问题中的标签。

我想跟踪有多少问题有标签。

这是我做的table

create table tag (name text primary key, questions counter);

现在我想添加一个标签到 table,但如果它已经存在则不应添加,它应该将现有标签的 questions 增加 1.

我认为它看起来像

update tag set questions = questions + 1 where name = ? if exists
insert tag (name, questions) values (?, 1) if not exists

但是上面给出了错误:

Conditional updates are not supported on counter tables

cassandra 中的更新工作方式为 upsert

如果存在相同的主键,它将被覆盖,否则将创建新的。

update tag set questions = questions + 1 where name = ?

只有这样才能满足要求