2014-10-28 61 views
0

我試圖更新表中的條目,以反映我已經存儲在另一個表中的內容,這是基於哪個類別的某個項目位於另一個表中。哦,我的物品都是由字母鍵(AKey)和數字鍵(NKey)鍵入的。基於在多個SQL表中查找更新單個條目

到目前爲止,我有

update itmHistory 
set ithLocation = storLocation from items, storage 
where ithAKey in (select storAKey from storage) 
    and ithNKey in (select storNKey from storage) 
    and storCategoryName = itmCategoryName; 

什麼結束了發生的事情是在項目表itmLocation的每個實例被設置到最後的條目,其中storCategoryName = itmCategoryName

任何想法,使每個更新,因爲它被發現?

編輯:表信息:

itmHistory: ithAKey varchar(3) PK 
      ithNKey int   PK 
      ithStart timestamp PK 
      ithEnd timestamp 
      ithLocation text 

items:  itmAKey varchar(3) PK 
      itmNKey int   PK 
      itmCategoryName text 

storage: storName text  PK 
      storCategoryName text 
      storLocation text 

(PK =主鍵)根據您的表信息,這個怎麼樣

+0

爲什麼'MySQL'在標籤列表中?這裏有關嗎? – vyegorov 2014-10-28 07:19:49

+0

請提供表格定義 – vyegorov 2014-10-28 07:20:56

回答

0

UPDATE ih 
SET ih.ithLocation = s.storLocation 
FROM itmHistory ih 
JOIN items i ON ih.ithAKey = i.itmAkey AND ih.ithNKey=i.itmNKey 
JOIN Storage s ON i.itemCategoryName = s.storCategoryName 
+0

我不完全確定一個連接是如何工作的,你能解釋一下嗎,這樣我就可以知道將來可以解決哪些問題? – 0pcode 2014-10-28 08:06:59