220

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?

0

8 Answers 8

461

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 
Sign up to request clarification or add additional context in comments.

4 Comments

It's one of those rare occasions where developers have thought like laymen.
Careful if you are unfamiliar with the update command... Without a WHERE clause, this command will update ALL records in the table.
Serious potential for wiping a lot of data, as @gmo cautioned. Consider making a backup of the database first, then run the query with a WHERE clause to limit it to one line. If you are happy with the result, remove the WHERE clause.
You may backup you table be fore the update action :)
37
UPDATE `table_name` SET `test` = `number` 

You can also do any mathematical changes in the process or use MySQL functions to modify the values.

Comments

14

try this:

update `list` set `test` = `number` 

1 Comment

list is <table name>
10

BEWARE : Order of update columns is critical

GOOD: What I want saves existing Value of Status to PrevStatus

UPDATE Collections SET PrevStatus=Status, Status=44 WHERE ID=1487496; 

BAD: Status & PrevStatus both end up as 44

UPDATE Collections SET Status=44, PrevStatus=Status WHERE ID=1487496; 

2 Comments

Why are you setting Status=44, though?
@sceletia just an arbitrary value to demonstrate the problem
10

try following:

UPDATE `list` SET `test` = `number` 

If list is table name and test and number are columns

it creates copy of all values from "number" and paste it to "test"

Comments

6

Following worked for me..

  1. Ensure you are not using Safe-mode in your query editor application. If you are, disable it!
  2. Then run following sql command

for a table say, 'test_update_cmd', source value column col2, target value column col1 and condition column col3: -

UPDATE test_update_cmd SET col1=col2 WHERE col3='value'; 

Good Luck!

Comments

0

IF Anyone wants to put Condition

UPDATE bohf SET Sq=IDNo WHERE Table = 'AOF' AND FormSq BETWEEN 13 AND 17

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-8
update `table` set `firstcolumn` = `secondcolumn` 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.