Mysql 使用另一个select查询的结果将数据插入表中

Mysql 使用另一个select查询的结果将数据插入表中,mysql,select,insert,Mysql,Select,Insert,我正在以下问题上寻求帮助: 我有两张桌子 表1列为项目ID,位置ID,数量 表2列为项目ID,位置1,位置2,位置3 我想将数据从表1(仅数量列)复制到表2(复制到位置1列)。两个表中的itemid是相同的(Table_1有重复的itemid),因此我想复制到一个新表中,并将所有数量保留在一行中,每个位置作为一列。我正在使用下面的查询,但它不起作用 INSERT INTO Table_2(location1) ( SELECT qty FROM Table_1 WHERE loca

我正在以下问题上寻求帮助: 我有两张桌子
表1
列为
项目ID
位置ID
数量

表2
列为
项目ID
位置1
位置2
位置3

我想将数据从
表1
(仅
数量
列)复制到
表2
(复制到
位置1
列)。两个表中的
itemid
是相同的(
Table_1
有重复的itemid),因此我想复制到一个新表中,并将所有数量保留在一行中,每个位置作为一列。我正在使用下面的查询,但它不起作用

INSERT INTO 
Table_2(location1) 
(
 SELECT qty 
 FROM Table_1 
 WHERE locationid = 1 AND Table_1.locationid = Table_2.locationid
)

如果
表2
为空,请尝试以下insert语句:

insert into table_2 (itemid,location1) 
select itemid,quantity from table_1 where locationid=1
update table_2 set location1=
(select quantity from table_1 where locationid=1 and table_1.itemid = table_2.itemid)
如果
table_2
已经包含
itemid
值,请尝试以下更新语句:

insert into table_2 (itemid,location1) 
select itemid,quantity from table_1 where locationid=1
update table_2 set location1=
(select quantity from table_1 where locationid=1 and table_1.itemid = table_2.itemid)

以下是此类查询的示例:

INSERT INTO [93275].[93276].[93277].[93278] ( [Mobile Number], [Mobile Series], [Full Name], [Full Address], [Active Date], company ) IN 'I:\For Test\90-Mobile Series.accdb
SELECT [1].[Mobile Number], [1].[Mobile Series], [1].[Full Name], [1].[Full Address], [1].[Active Date], [1].[Company Name]
FROM 1
WHERE ((([1].[Mobile Series])="93275" Or ([1].[Mobile Series])="93276")) OR ((([1].[Mobile Series])="93277"));OR ((([1].[Mobile Series])="93278"));

欢迎来到堆栈溢出。在这个问题上有两件事。1) 代码应该格式化。将其缩进四个空格。2) 最好解释一下为什么你的代码解决了提问者的问题。如果你有相同的列,这就足够了:
insert into table_2选择itemid,quantity from table_1,locationid=1