将同一表中的值从一列复制到另一列

时间:2022-09-24 19:00:53

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?

我应该使用什么mysql查询?

7 个解决方案

#1


265  

UPDATE `table` SET test=number

#2


23  

UPDATE `table_name` SET `test` = `number`

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

您还可以在此过程中进行任何数学更改,或者使用MySQL函数修改值。

#3


8  

try this:

试试这个:

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

#4


2  

BEWARE : Order of update columns is critical

注意:更新列的顺序非常重要

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

好:我想保存状态的现有值到PrevStatus

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

BAD: Status & PrevStatus both end up as 44

坏:状态和PrevStatus最后都是44

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

#5


1  

try following:

试一试:

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

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

它从“number”创建所有值的副本并将其粘贴到“test”

#6


1  

Following worked for me..

以下为我工作。

  1. Ensure you are not using Safe-mode in your query editor application. If you are, disable it!
  2. 确保在查询编辑器应用程序中没有使用安全模式。如果你是,禁用它!
  3. Then run following sql command
  4. 然后按照sql命令运行

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

对于一个表,例如'test_update_cmd',源值列col2,目标值列col1和条件列col3: -

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

Good Luck!

好运!

#7


-6  

you can do it with Procedure also so i have a procedure for this

你也可以用程序来做所以我有一个程序

 DELIMITER $$
 CREATE PROCEDURE copyTo()
       BEGIN
               DECLARE x  INT;
            DECLARE str varchar(45);
              SET x = 1;
            set str = '';
              WHILE x < 5 DO
                set  str = (select source_col from emp where id=x);
            update emp set target_col =str where id=x;      
            SET  x = x + 1;
                END WHILE;

       END$$
   DELIMITER ;

#1


265  

UPDATE `table` SET test=number

#2


23  

UPDATE `table_name` SET `test` = `number`

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

您还可以在此过程中进行任何数学更改,或者使用MySQL函数修改值。

#3


8  

try this:

试试这个:

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

#4


2  

BEWARE : Order of update columns is critical

注意:更新列的顺序非常重要

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

好:我想保存状态的现有值到PrevStatus

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

BAD: Status & PrevStatus both end up as 44

坏:状态和PrevStatus最后都是44

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

#5


1  

try following:

试一试:

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

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

它从“number”创建所有值的副本并将其粘贴到“test”

#6


1  

Following worked for me..

以下为我工作。

  1. Ensure you are not using Safe-mode in your query editor application. If you are, disable it!
  2. 确保在查询编辑器应用程序中没有使用安全模式。如果你是,禁用它!
  3. Then run following sql command
  4. 然后按照sql命令运行

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

对于一个表,例如'test_update_cmd',源值列col2,目标值列col1和条件列col3: -

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

Good Luck!

好运!

#7


-6  

you can do it with Procedure also so i have a procedure for this

你也可以用程序来做所以我有一个程序

 DELIMITER $$
 CREATE PROCEDURE copyTo()
       BEGIN
               DECLARE x  INT;
            DECLARE str varchar(45);
              SET x = 1;
            set str = '';
              WHILE x < 5 DO
                set  str = (select source_col from emp where id=x);
            update emp set target_col =str where id=x;      
            SET  x = x + 1;
                END WHILE;

       END$$
   DELIMITER ;