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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:56:47  来源:igfitidea点击:

copy one column from one table to another

mysqlsql-update

提问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:

这里有两个选项:

  1. update your tables to use BuildIDas a primary key (to avoid duplicates)
  2. 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
    );
    
  1. 更新您的表以用BuildID作主键(以避免重复)
  2. 更新您的子查询以仅返回一个结果

    UPDATE results SET results.platform_to_insert = (
        SELECT correct_platform
        FROM build
        WHERE results.BuildID=build.BuildID LIMIT 1
    );