Brad
Brad

Reputation: 12272

need to move data from one field to another, within the same table

I want to move all data from field_id_41 into field_id_380, where weblog_id = 191

Those two fields belong to the same table: exp_weblog_data

I do not want to run anything without asking here, I started to put this together:

UPDATE 
  exp_weblog_data 
SET 
  field_id_380 = (SELECT field_id_41 FROM exp_weblog_data 
  WHERE (field_id_41 != '' and weblog_id = 191)) 
WHERE 
  weblog_id = 191

Upvotes: 7

Views: 10424

Answers (3)

saepulloh
saepulloh

Reputation: 51

maybe like this:

UPDATE 
  exp_weblog_data 
SET 
  field_id_380 =field_id_41,   field_id_41 = ''
WHERE 
  weblog_id = 191
AND 
  field_id_41 != ''

Upvotes: 5

Frank Allenby
Frank Allenby

Reputation: 4392

What I'd do if I were you is make a backup of that database and host it on another(or the same) server. Once you've done that you can literally go nuts and experiment at will without fear of losing any real data. That query looks fine to me, though.

Upvotes: 0

Konerak
Konerak

Reputation: 39773

Really no need for the subquery. You can just take the value for the other column in the same row, and set it in the first column:

UPDATE 
  exp_weblog_data 
SET 
  field_id_380 =field_id_41 
WHERE 
  weblog_id = 191
AND 
  field_id_41 != ''

Upvotes: 15

Related Questions