基于在多个SQL表中查找更新单个条目
问题描述:
我试图更新表中的条目,以反映我已经存储在另一个表中的内容,这是基于哪个类别的某个项目位于另一个表中。哦,我的物品都是由字母键(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 =主键)根据您的表信息,这个怎么样
答
?
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
为什么'MySQL'在标签列表中?这里有关吗? – vyegorov 2014-10-28 07:19:49
请提供表格定义 – vyegorov 2014-10-28 07:20:56