Reputation: 41
This is the query I'm trying to accomplish:
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);
I can't get this to work...receiving a syntax error on the first parenthesis. So, I figured I'd try on just one field. I tried this:
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);
This however returns and error 1242: Subquery returns more than 1 row. That seems silly. I know it's going to return more than one row...I want it to because I need to update multiple rows.
What am I missing?
Upvotes: 3
Views: 18634
Reputation: 27854
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.
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.
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;
Upvotes: 12