2014-01-22 59 views
-1

我正在嘗試使用INNER JOIN更新另一個表中的表中的一個字段。這裏是代碼:使用內部連接的更新字段

UPDATE TestResults 
INNER JOIN Distractors 
ON TestResults.DistractorID = Distractors.ID 
SET TestResults.DistractorValue = Distractors.DistractorValue 

這不起作用我不知道爲什麼!任何想法?當我運行查詢,我得到以下錯誤

There was an error parsing the query. [ Token line number = 2,Token line offset = 1,Token  in error = INNER ] 
+1

Pleace指定數據庫供應商。 MSSQL? MySQL的? –

回答

1

並非所有的數據庫都支持join語法與update。當他們這樣做時,語法不同。這裏是一個方式做您的查詢,而無需使用標準的SQL明確join

UPDATE TestResults 
    set DistractorValue = (select max(d.DistractorValue) 
          from Distractors d 
          where TestResults.DistractorValue = d.DistractorValue 
         ) 
where exists (select 1 
       from Distractors d 
       where TestResults.DistractorValue = d.DistractorValue 
      ); 

max()是否有可能不止一個匹配行時才需要。

僅當join用於過濾和匹配時才需要where

1
UPDATE TestResults 
SET TestResults.DistractorValue = Distractors.DistractorValue 
FROM TestResults 
INNER JOIN Distractors 
ON TestResults.DistractorID = Distractors.ID 
0

您使用更新後的表內連接從句

例如:

UPDATE TestResults 
    SET TestResults.DistractorValue = Distractors.DistractorValue 
    FROM TestResults INNER JOIN Distractors 
    ON TestResults.DistractorID = Distractors.ID