2017-08-02 41 views
0

我具有中間表:由更新替換選擇表

text_mining_molecule

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

和其他兩個表:

表分子:

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

表jsonTextMining:

id | title | molecule_name      | others … 
---|------- |-------------------------------------|------ 
1 | title1 | colchicine, cellulose, acid, caféine| others … 

text_mining_molecule需要被插入時,從2個人桌json_text_miningmolecule在列表中選擇與ID的選擇。

其實,有一個已經從json_text_mining插入所有行text_mining 4.

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

+0

一些FK ..什麼是text_mining_molecule和s.id之間的關係,m.id .. –

+0

'json_text_mining'是一個臨時表,其中所有的文章都是?等待驗證。當有人點擊一個驗證按鈕時,'json_text_mining'文章被拷貝到'text_mining'表中,但是中間表中的關係不存在 – Gy0m

+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