且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-09 20:32:34

您想要的查询如下所示:

The query you want would look something like this:

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;

注意,您将不得不用我未写的其余列关联替换....

Observe you will have to replace ... with the rest of the column associations I didn't write.

但是请注意,有些信息告诉我此查询将对您的数据库执行非常错误的操作,因为您没有使用唯一键来关联表.如果有两个记录具有相同的ASCNCOAClean.firstname,您肯定会丢失数据.

But be careful with that, something tells me this query is going to do something very wrong to your database, because you are not relating the tables using a unique key. If there are two records with the same ASCNCOAClean.firstname you certainly will have loss of data.

还请注意,它将更新amdashboard上的现有记录,而不添加新记录.如果您打算将数据从ASCNCOAClean迁移到amdashboard,并假设amdashboard是一个全新的空表,那么您想要的查询是这样的:

Also observe that it is going to update existing records on amdashboard, not add new ones. If your intention is to migrate data from ASCNCOAClean to amdashboard, assuming amdashboard is a brand new, empty table, then the query you want is this:

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;