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

如何从mysql中的另一个表更新多个字段?,mysql,sql,sql-update,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) = (sele

这是我试图完成的查询:

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;
注意,您将不得不用我没有编写的其余列关联替换

但是要小心,有些东西告诉我这个查询会对数据库造成很大的错误,因为您没有使用唯一的键来关联表。如果有两条记录具有相同的
asncocaclean.firstname
,则肯定会丢失数据

还要注意,它将更新
amdashboard
上的现有记录,而不是添加新记录。如果您打算将数据从
ascnaclean
迁移到
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;

您可以发布一些示例数据或架构结构吗?谢谢…我基本上已经找到了解决方案。更新amdashboard内部连接ascnaclean on ascnaclean.firstname=amdashboard.actorsfirst和ascnaclean.lastname=amdashboard.actorslast set/*amdashboard.ascd=ascnaclean.id,*/*amdashboard.ascfirst=ascnaclean.firstname,*/amdashboard.asclast=ascnaclean.lastname,amdashboard.ascothername=ascnaclean.listingspowsename,amdashboard.ascad1=ascnaclean.add1,amdashboard.ascad2=ascnaclean.add2,amdashboard.asccity=ascnaclean.city,amdashboard.ascstate=ascnaclean.state,amdashboard.asczip=ascnaclean.zip;对不起,我仍然习惯于这个工具…我知道它的格式不好…我尝试在姓氏和名字上链接,是的,我想我知道我有重复数据的问题。这是一个客户的清理工作…我想更新匹配的记录,然后插入不匹配的记录。