通过更新替换选择表

时间:2017-08-02 09:52:25

标签: sql postgresql

我有一张中间表:

text_mining_molecule

|text_mining_id| molecule_id |
| -------------| ----------  |
|  ID          |  ID         |

和另外两张表:

TableMolécules:

id  | main_name | others …
--- | --------- | ------
1   | caféine   | others …

表jsonTextMining:

id | title  | molecule_name                       | others …
---|------- |-------------------------------------|------
1  | title1 | colchicine, cellulose, acid, caféine| others …
如果在列表中选择了来自其他2个表text_mining_moleculejson_text_mining的ID,则需要插入

molecule

实际上,当选择低于4的分数时,有一个下拉列表已插入json_text_miningtext_mining的所有行。

INSERT INTO text_mining (id, solrid, originalpaper, annotatedfile, title, keyword, importantsentence, version, klimischscore, moleculename, synonymname, validation)
            SELECT id, solrid, originalpaper, annotatedfile, title, keyword, importantsentence, version, klimischscore, molecule_name, synonym_name, validation
            FROM json_text_mining WHERE klimischscore < 4

这有效,但我需要text_mining_molecule填写相关的ID,所以我也有这部分代码:

SELECT s.id, m.id
          FROM (SELECT id, regexp_split_to_table(molecule_name,', ') AS m_name
          FROM json_text_mining) AS s, molecule m
          WHERE m.main_name = s.m_name;

如何使用text_mining_molecule而不是insert直接更新select表?

1 个答案:

答案 0 :(得分:0)

使用CTE。例如,如果text_mining_molecule.molecule引用molecule.id,那就像是:

with c as (
SELECT s.id sid, m.id mid
          FROM (SELECT id, regexp_split_to_table(molecule_name,', ') AS m_name
          FROM json_text_mining) AS s, molecule m
          WHERE m.main_name = s.m_name
)
update text_mining_molecule t
set sid = c.sid
from c
where t.molecule = c.mid