想象一个存储问题标签的表,比如堆栈溢出问题的标签。
我想跟踪有多少问题有标签。
我做了这张桌子
create table tag (name text primary key, questions counter);
现在我想在表格中添加一个标签,但是如果它已经存在则不应该添加,它应该通过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 = ?
只有这样才能满足要求