mysql - 你如何从mysql中的另一个表更新多个字段?

标签 mysql sql sql-update

这是我要完成的查询:

update amdashboard
set (ASCID, ASCFirst, ASCLast, ASCOtherName, ASCAdd1, ASCAdd2,
     ASCCity, ASCState, ASCZip, ASCZip4, ASCY2007, ASCY2008, ASCY2009,
     ASCY2010, ASCY2011, ASCY2012, ASCEthnicity, ASCGender, ASCMaritalStatus)
= (select id, firstname, lastname, listingspousename, add1, add2,
          city, state, zip, zip4, y2007, y2008, y2009,
          y2010, y2011, y2012, Ethnicity, Gender, MaritialStatus
     from ASCNCOAClean
          inner join amdashboard
          on ASCNCOAClean.firstname = amdashboard.actorsfirst
          and ascncoaclean.lastname = amdashboard.actorslast)
    where exists (select id, firstname, lastname, listingspousename,
                         add1, add2, city, state, zip, zip4, y2007, y2008,
                         y2009, y2010, y2011, y2012, Ethnicity, Gender,
                         MaritialStatus
                    from ASCNCOAClean
                         inner join amdashboard
                         on ASCNCOAClean.firstname = amdashboard.actorsfirst
                         and ascncoaclean.lastname = amdashboard.actorslast);

我无法让它工作...在第一个括号中收到语法错误。所以,我想我会尝试只在一个领域。我试过这个:

update amdashboard
set ascid = (select ascncoaclean.id
         from ASCNCOAClean 
         where ASCNCOAClean.firstname = amdashboard.actorsfirst
                           and ascncoaclean.lastname = amdashboard.actorslast)
where exists (select ascncoaclean.id
         from ASCNCOAClean 
         where ASCNCOAClean.firstname = amdashboard.actorsfirst
                           and ascncoaclean.lastname = amdashboard.actorslast);

然而,这会返回错误 1242:子查询返回多于 1 行。这看起来很傻。我知道它会返回不止一行......我想要它,因为我需要更新多行。

我错过了什么?

最佳答案

您想要的查询看起来像这样:

UPDATE amdashboard a, ASCNCOAClean b SET
   a.ASCID            = b.id,
   a.ASCFirst         = b.firstname,
   a.ASCLast          = b.lastname,
   a.ASCOtherName     = b.listingspousename,
   ...
   a.ASCMaritalStatus = b.MaritialStatus
WHERE a.actorsfirst = b.firstname;

请注意,您必须将 ... 替换为我没有编写的其余列关联。

但是要小心,有些东西告诉我这个查询会对你的数据库做一些非常错误的事情,因为你没有使用唯一键关联表。如果有两条具有相同 ASCNCOAClean.firstname 的记录,您肯定会丢失数据。

同时观察到它会更新 amdashboard 上的existing 记录,而不是添加新记录。如果你的目的是将数据从ASCNCOAClean迁移到amdashboard,假设amdashboard是一个全新的空表,那么你想要的查询是这样的:

INSERT INTO amdashboard (
    ASCID, ASCFirst, ASCLast, ASCOtherName, ASCAdd1, ASCAdd2, ASCCity, ASCState, 
    ASCZip, ASCZip4, ASCY2007, ASCY2008, ASCY2009, ASCY2010, ASCY2011, ASCY2012,
    ASCEthnicity, ASCGender, ASCMaritalStatus
)
SELECT
    id, firstname, lastname, listingspousename, add1, add2, city, state,
    zip, zip4, y2007, y2008, y2009, y2010, y2011, y2012, Ethnicity, Gender,
    MaritialStatus
FROM ASCNCOAClean;

关于mysql - 你如何从mysql中的另一个表更新多个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15755024/

相关文章:

php - SQL ERROR = 'Backpack' 中的未知列 'where clause' ...提示?

sql - 如何合并多个 SELECT 结果?

mysql - 用另一个表的数据更新 MySQL 表

php - AES-256加密,PHP转MySQL

php - 页面加载后的 Ajax 请求

mysql - 关系表设计: prevent circular references

MySQL 表不会更新多行

ms-access - 错误 : This recordset is not updateable

使用 "IN"子句的 MySql JOIN 多个表

php - 从一个字段设置数据库中的多个字段