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

问题描述 投票:0回答:1

想象一个存储问题标签的表,比如堆栈溢出问题的标签。

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

我做了这张桌子

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 cql cassandra-3.0
1个回答
1
投票

cassandra的更新与upsert一样。

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

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

只有这样才能满足要求

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.