Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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
like image 365
Brad Avatar asked Sep 15 '11 17:09

Brad


People also ask

How do I move data from one column to another column?

Double-click the cell that contains the data that you want to move or copy. You can also edit and select cell data in the formula bar. Select the row or column that you want to move or copy. In the cell, click where you want to paste the characters, or double-click another cell to move or copy the data.

How can I copy data from one column to another in the different table?

Click the tab for the table with the columns you want to copy and select those columns. From the Edit menu, click Copy. Click the tab for the table into which you want to copy the columns. Select the column you want to follow the inserted columns and, from the Edit menu, click Paste.

How do you UPDATE column values in the same table?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.

How do I transfer data from one column to another in MySQL?

To copy from one column to another, you can use INSERT INTO SELECT statement.


2 Answers

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 != ''
like image 101
Konerak Avatar answered Sep 23 '22 06:09

Konerak


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 != ''
like image 32
saepulloh Avatar answered Sep 26 '22 06:09

saepulloh