2017-04-11 47 views
1

我有一個在列出對應於信貸員數字的表名prequals_created命名loanofficer列。我想更新此列有信貸員名稱,而不是數量,它位於一個名爲appusers表,其中fullnameloanofficer ID匹配。我試圖這樣做,首先使用SELECT通過使用名爲loan_pairing的「中間」表來生成信貸員名稱和id作爲一對錶。然而,我的語法有些問題。這裏是我的代碼:UPDATE SQL柱選擇值

UPDATE 
    prequals_created AS pc 
SET 
    pc.loanofficer = lonames.fullname 
FROM 
    (SELECT DISTINCT a.fullname, lp.loanofficer 
    FROM appusers AS a 
    JOIN loan_pairing AS lp 
     ON a.id = lp.loanofficer 
    JOIN prequals_created AS pc 
     ON lp.loanofficer = pc.loanofficer) AS lonames 
WHERE 
    pc.loanofficer = lonames.loanofficer 

嵌套在FROM語句中的SELECT語句是正確的,並返回各自的ID成對信貸員的姓名。

這裏關於我的語法到底是什麼?

+0

是位於您的全名列在用戶? –

+0

是的,它是(額外字符) – Jodo1992

+1

我覺得loan_pairing表亙古不抱什麼特別做這個工作,即APPUSER的id是prequals_created表作爲loanofficer,你想用全名是在appsuser表 –

回答

1

在多表更新語句MySQL聯接的SET前走。我想,這應該爲你工作:

UPDATE prequals_created AS pc 
     JOIN loan_pairing AS lp 
      ON lp.loanofficer = pc.loanofficer 
     JOIN appusers AS a 
      ON a.id = lp.loanofficer 
SET  pc.loanofficer = a.fullname; 
+0

謝謝你做的,這個工作奇妙,我只需要在修改後還包括WHERE語句 – Jodo1992

0
UPDATE 
    prequals_created AS pc 
SET 
    pc.loanofficer = lonames.fullname 
FROM 
    appusers AS a 
    JOIN 
    loan_pairing AS lp 
     ON a.id = lp.loanofficer 
    JOIN prequals_created AS pc 
     ON lp.loanofficer = pc.loanofficer 
; 
0

刪除不必要的連接表loan_pairing,並讓您的查詢有效和高效 這是你想要的

UPDATE 
     pc 
    FROM appusers AS a 
     JOIN prequals_created AS pc 
      ON a.id = pc.loanofficer 
SET 
     pc.loanofficer = a.fullname