Question

Redesigning a database layout and i've hit a road block:

The original table now named Items_Old had these 3 columns of importance:

Name | Width | Length

I have since made a new table called ItemSizes:

ID | Width | Length

and a last called Items:

Name | SizeID

I am trying to figure out how to select the SizeID from the ItemSizes table into Items Table

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

I have gotten as far as being able to get a list of SizeID and Items_Old.Name with the following:

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

but when I try something like :

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

I get a syntax error because of the second Where I also tried

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

but got an error near "("

Was it helpful?

Solution

In an UPDATE statement, you can look up values with a correlated subquery:

UPDATE Items
SET SizeID = (SELECT ID
              FROM ItemSizes
              JOIN Items_Old USING (Width, Length)
              WHERE Name = Items.Name)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top