给出以下架构:
_e()
鉴于文档document(id, path);
term(id, description);
term_document(id_term,id_document,frequency);
和字词path
,如果我在表description
中没有term
和document
的记录我想在频率= 1的表term_documento
中插入,否则,我只想增加term_document
值。
我到目前为止所得到的是:
frequency
这使我在term_document中没有记录的情况,但我不知道如何增加以满足两者。
答案 0 :(得分:1)
假设您对term_document(id_term,id_document)有唯一约束,则可以在插入中使用on conflict
子句:
insert into term_document(id_term, id_document, frequency)
select t.id, d.id, 1
from term t
cross join document d
where t.description = 'to'
and d.path = '/home/rogger/Projetos/projeto-ori-ufu/src/main/resources/d1.txt'
on conflict (id_term, id_document) do update set frequency = frequency + 1;
我使用了交叉连接(仅在现代语法中)。如果两个表之间实际存在关系,则需要在这些列上加入它们。