43

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

linuxbuild
  • 15,843
  • 6
  • 60
  • 87
William Macdonald
  • 2,139
  • 2
  • 21
  • 27

5 Answers5

190
UPDATE table1 t1, table2 t2
SET t1.field_to_change = t2.field_with_data
WHERE t1.field1 = t2.field2;
Jomoos
  • 12,823
  • 10
  • 55
  • 92
MCurbelo
  • 4,097
  • 2
  • 26
  • 21
23
UPDATE book_details AS bd, book_details_old AS old
SET bd.live=1  
WHERE bd.isbn13=old.isbn13  
AND old.live=1;
castis
  • 8,154
  • 4
  • 41
  • 63
Rob
  • 3,687
  • 2
  • 32
  • 40
17

Just a litle change and you got it:

UPDATE book_details
SET live = 1 
WHERE ISBN13 in (SELECT ISBN13 FROM book_details_old WHERE live = 1);
Ricardo Acras
  • 35,784
  • 16
  • 71
  • 112
6

To update a table from data in another table:

UPDATE table1, table2
SET table1.field1 = table2.field1
WHERE table1.id = table2.id;

For example:

UPDATE transaction, member
SET transaction.Memberid = member.memberId
WHERE transaction.CardId = member.CardId;
simhumileco
  • 31,877
  • 16
  • 137
  • 115
luxknight_007
  • 109
  • 1
  • 3
-3

To Update data From Other Table

UPDATE  tab t1, tab t2
SET     t1.company_name = t2.company_name
WHERE   t1.id = t2.id