Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy values from one column to another in the same table

Tags:

database

mysql

How can I make a copy values from one column to another?

I have:

Database name: list  ------------------- number | test ------------------- 123456 | somedata 123486 | somedata1 232344 | 34 

I want to have:

Database name: list  ---------------- number | test ---------------- 123456 | 123456 123486 | 123486 232344 | 232344 

What MySQL query should I have?

like image 882
Lucas Avatar asked Jan 25 '12 11:01

Lucas


People also ask

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 copy values from one column to another?

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. or press Ctrl+V. Press ENTER.

How do I assign a value from one column to another in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

How do I copy the value of one column to another field in PostgreSQL?

Use the 'UPDATE' SQL Keyword to Copy the Contents of a PostgreSQL Column. UPDATE some_table SET col_NEW = col_str; This statement should return a response saying UPDATE , followed by an integer value indicating the number of records that were copied (e.g. UPDATE 3 ).


1 Answers

Short answer for the code in question is:

UPDATE `table` SET test=number 

Here table is the table name and it's surrounded by grave accent (aka back-ticks `) as this is MySQL convention to escape keywords (and TABLE is a keyword in that case).


BEWARE!

This is pretty dangerous query which will wipe everything in column test in every row of your table replacing it by the number (regardless of it's value)

It is more common to use WHERE clause to limit your query to only specific set of rows:

UPDATE `products` SET `in_stock` = true WHERE `supplier_id` = 10 
like image 195
4 revs, 2 users 81% Avatar answered Oct 05 '22 20:10

4 revs, 2 users 81%