Take the 2-minute tour ×

I'm trying to update several rows with email addresses. T1.email is to be updated with T2.email based on T1.name existing in T2.name.

Currently, the query looks like this.

SET T1.email = T2.email
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.name = T2.name
WHERE T1.name = T2.name
AND (Some conditions)
LIMIT 1398

A similiar question is asked here, but a syntax error is given. SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?

I've also tried updating with ANY.

SET Table1.email = ANY
        SELECT Table2.email FROM Table2 
        WHERE Table1.accountid = 901234 
        AND Table2.pid = 123 
        AND Table2.email IS NOT NULL 

WHERE Table1.name IN
        SELECT Table2.name FROM Table2
        WHERE Table1.accountid = 19574
        AND Table2.pid = 123 
        AND Table2.email IS NOT NULL

LIMIT 1398

Currently, this returns an error "SQL Error (1242): Subquery returns more than 1 row".

May be the beginning of a copy and paste job!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

As detailed under UPDATE Syntax, for MySQL you want:

UPDATE Table1 T1 JOIN Table2 T2 USING (name)
SET    T1.email = T2.email
WHERE  (Some conditions)
LIMIT  1398
share|improve this answer
Works! Thanks a million! –  parseMaestro63 Dec 26 '12 at 16:34

Your Answer


