MySQL 将一列从一张表复制到另一张表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2015527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
copy one column from one table to another
提问by JPro
I am confused about how to copy a column from one table to another table using where. I wrote SQL query but it says transaction lock time exceeded or query returns more than one row.
using mysql
Basically,
I have:
我对如何使用 where 将一列从一个表复制到另一个表感到困惑。我写了 SQL 查询,但它说超出事务锁定时间或查询返回多于一行。
使用 mysql
基本上,
我有:
Table 1: Results
BuildID platform_to_insert
Table 2: build
BuildID correct_platform
update results set results.platform_to_insert
= (select correct_platform from
build where results.BuildID = build.BuildID)
回答by Benoit Vidis
I do not believe you need a sub query.
我不相信你需要一个子查询。
UPDATE results, build
SET results.platform_to_insert = build.correct_platform
WHERE results.BuildID = build.BuildID
回答by tmpvar
There are two options here:
这里有两个选项:
- update your tables to use
BuildID
as a primary key (to avoid duplicates) update your subquery to only return one result
UPDATE results SET results.platform_to_insert = ( SELECT correct_platform FROM build WHERE results.BuildID=build.BuildID LIMIT 1 );
- 更新您的表以用
BuildID
作主键(以避免重复) 更新您的子查询以仅返回一个结果
UPDATE results SET results.platform_to_insert = ( SELECT correct_platform FROM build WHERE results.BuildID=build.BuildID LIMIT 1 );