Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update mysql table with data from another table

Is it possible to run an UPDATE command on mysql 5.0 with a sub select.

The command I would like to run is this:

UPDATE book_details SET live = 1  WHERE ISBN13 = '(SELECT ISBN13 FROM book_details_old WHERE live = 1)'; 

ISBN13 is currently stored as a string.

This should be updating 10k+ rows.

Thanks,

William

like image 218
William Macdonald Avatar asked Jan 05 '09 23:01

William Macdonald


People also ask

How do I select data from one table and insert another in MySQL?

The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.


1 Answers

UPDATE table1 t1, table2 t2 SET t1.field_to_change = t2.field_with_data WHERE t1.field1 = t2.field2; 
like image 140
MCurbelo Avatar answered Oct 22 '22 10:10

MCurbelo