2

I've been reading all the questions on here but I still don't get it

I have two identical tables of considerable size. I would like to update table packages_sorted with data from packages_sorted_temp without destroying the existing data on packages_sorted

Table packages_sorted_temp contains data on only 2 columns db_id and quality_rank

Table packages_sorted contains data on all 35 columns but quality_rank is 0

The primary key on each table is db_id and this is what I want to trigger the ON DUPLICATE KEY UPDATE with.

In essence how do I merge these two tables by and change packages_sorted.quality_rank of 0 to the quality_rank stored in packages_sorted_temp under the same primary key

Here's what's not working

INSERT INTO `packages_sorted` ( `db_id` , `quality_rank` )
SELECT `db_id` , `quality_rank`
FROM `packages_sorted_temp` ON DUPLICATE
KEY UPDATE `packages_sorted`.`db_id` = `packages_sorted`.`db_id` 
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
  • 1
    Is this related to php? i.e. does it work from MySQL workbench and not from php? if so, php tag can be removed – melihcelik Nov 27 '11 at 09:54

2 Answers2

3
update packages_sorted , packages_sorted_temp 
set packages_sorted.quality_rank = packages_sorted_temp.quality_rank 
where packages_sorted.db_id = packages_sorted_temp.db_id 
Surasin Tancharoen
  • 5,520
  • 4
  • 32
  • 40
2

You can use Update join like this:

Update packages_sorted p
    inner join packages_sorted_temp temp
    on p.db_id = temp.db_id 
set p.db_id = temp.db_id, p.quality_rank = temp.quality_rank 
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164