Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Data from one column to another similar column in another table MYSQL

Tags:

mysql

I am currently working on a webbased systen using a Mysql db.

I realised that I had initially set up the columns within the tables incorrectly and

I now need to move the data from one table column (receiptno) in table (clients) into a similar table column(receiptno) in table (revenue).

I am still quite inexperienced with Mysql and therefore I dont know the the mysql syntax to accomplish this.

Can I get some help on it.

Thanks

like image 415
Stanley Ngumo Avatar asked May 17 '12 11:05

Stanley Ngumo


People also ask

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.

How do I copy data from one table to another with different columns?

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 I insert data from one column to another column in SQL?

UPDATE table SET columnB = columnA; This will update every row. This will also work if you want to transfer old value to other column and update the first one: UPDATE table SET columnA = 'new value', columnB = columnA . Like other answer says - don't forget the WHERE clause to update only what's needed.


1 Answers

If you simply wanted to insert the data into new records within the revenue table:

INSERT INTO revenue (receiptno) SELECT receiptno FROM clients;

However, if you want to update existing records in the revenue table with the associated data from the clients table, you would have to join the tables and perform an UPDATE:

UPDATE revenue JOIN clients ON **join_condition_here**
SET    revenue.receiptno = clients.receiptno;

Learn about SQL joins.

like image 134
eggyal Avatar answered Oct 02 '22 20:10

eggyal