2017-08-07 183 views
1

我有四個名稱爲candidate,program_type,program_of_interest和desired_intake的表。表候選人與三個表中的其餘部分有多對一的關係。如何使用另一個表中的值更新字段值

候選表看起來像這樣: enter image description here

的program_type表: enter image description here

的program_of_interest表:

的desired_intake表: enter image description here

我想取代程序am_type,program_of_interest,desired_index在候選表中具有相應值的id。 我可以根據需要在此線程的幫助下選擇值SQL Replace multiple variables from another table in query result。這裏是我的解決方案選擇:

SELECT 
    c.id, 
    p.value as 'Program type', 
    p1.value as 'Program of interest', 
    d.value as 'Desired intake' 
FROM candidate c 
JOIN program_type p on p.id = c.program_type 
JOIN program_of_interest p1 on p1.id = c.program_of_interest 
JOIN desired_intake d on d.id = c.desired_intake 

我的問題是我如何與它們各自的值替換候選表的ID?

回答

1

你可以使用上的更新basend加入成爲

Update candidate c 
    INNER JOIN program_type p on p.id = c.program_type 
    INNER JOIN program_of_interest p1 on p1.id = c.program_of_interest 
    INNER JOIN desired_intake d on d.id = c.desired_intake 
    set c.program_type = p.value, 
     c.program_of_interest = p1.value, 
     c.desired_intake = d.value 

,但似乎很奇怪,你要更新如:program_type與值(c.program_type = p.value) 和你正在使用的捧場program_type( p.id = c.program_type)

+0

嗨,感謝您的快速響應。然而,查詢返回錯誤 – utkarsh2k2

+0

這裏是錯誤'MySQL說:文檔 #1064 - 你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'p.id = c.program_type上JOIN program_type p'附近使用正確的語法' JOIN program_of_interest p1 on p'at line 5' – utkarsh2k2

+0

answer updated .. – scaisEdge

相關問題