How to update a table based on the output of a select?

I need to update a table based on the output of a select.
Example:
Select a.fld1, a.fld2 from db.tableA a where a.fld3 = 'blahblah'
The output might be something like
fld1           fld2
'TEXT1'      123
'TEXT2'      432
'TEXT3'       534
etc..

I'd like to be able to update tableB based on the results of this select.

update db.tableB b set b.fld1 = a.fld1 where b.fld2 = a.fld2

how would write such an update command?

i hope i'm  explaining myself clearly - ?
AlgorithmixAsked:
Who is Participating?
 
joebednarzConnect With a Mentor Commented:
UPDATE db.tableB b SET b.fld1 = (SELECT a.fld1 FROM tableA a WHERE b.fld2 = a.fld2);
0
 
a_twixt_in_the_taleCommented:
you might want to add one more clause to joebednarz's solution
 
... where b.fld2 in (select a.fld2 from tableA)

cheers!
Don
0
 
AlgorithmixAuthor Commented:
Thanks guys!
0
All Courses

From novice to tech pro — start learning today.