我寻求帮助的以下问题:我有两个表 Table_1列itemid,locationid,quantity
Table_1
itemid
locationid
quantity
Table_2列itemid,location1,location2,location3
Table_2
location1
location2
location3
我想将数据从Table_1(仅quantity列)复制到Table_2(到location1列)。该itemid均在表中相同(Table_1有重复项的ID),所以这就是我要复制到新表,并保持在一个单一的行中的所有数量与每个位置为一列的原因。我正在使用以下查询,但它不起作用
INSERT INTO Table_2(location1) ( SELECT qty FROM Table_1 WHERE locationid = 1 AND Table_1.locationid = Table_2.locationid )
如果table_2为空,请尝试以下插入语句:
table_2
insert into table_2 (itemid,location1) select itemid,quantity from table_1 where locationid=1
如果table_2已经包含itemid值,请尝试以下更新语句:
update table_2 set location1= (select quantity from table_1 where locationid=1 and table_1.itemid = table_2.itemid)