0

重新设计数据库布局,我遇到了障碍:

现在命名的原始表Items_Old具有以下 3 列的重要性:

Name | Width | Length

从那以后,我制作了一个新表,名为ItemSizes

ID | Width | Length

最后一个叫Items

Name | SizeID

我想弄清楚如何从ItemSizes表中选择 SizeID 到Items表中

where Items_Old.Name = Items.Name and Items_Old.Width = ItemSizes.Width 
      AND Items_Old.Length = ItemSizes.Length

我已经能够获得 SizeID 和 Items_Old.Name 的列表,其中包含以下内容:

Select IST.ID , Old.Name
From ItemSizes AS IST, Items_Old AS Old
Where IST.Width = Old.Width 
       And  IST.Length = Old.Length 

但是当我尝试类似的东西时:

Insert Into Items
(SizeID)
Select IST.ID , Old.Name
From ItemSizes AS IST, Items_Old AS Old
Where IST.Width = Old.Width 
       And  IST.Length = Old.Length
Where Items.Name = Info.Name

由于第二个我也尝试过的地方,我得到了一个语法错误

Insert Into Items
(SizeID)
(Select IST.ID , Old.Name
From ItemSizes AS IST, Items_Old AS Old
Where IST.Width = Old.Width 
       And  IST.Length = Old.Length) As Info 
Where Items.Name = Info.Name

但出现错误near "("

4

1 回答 1

1

在 UPDATE 语句中,您可以使用相关子查询查找值:

UPDATE Items
SET SizeID = (SELECT ID
              FROM ItemSizes
              JOIN Items_Old USING (Width, Length)
              WHERE Name = Items.Name)
于 2014-03-25T21:43:25.007 回答