Copiar valores de una columna a otra en la misma tabla


¿Cómo puedo hacer una copia de valores de una columna a otra? Tengo:

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

Quiero tener:

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

¿Qué consulta mysql debo tener?

Author: Lucas, 2012-01-25

7 answers

UPDATE `table` SET test=number
 283
Author: Juicy Scripter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-25 11:30:06
UPDATE `table_name` SET `test` = `number`

También puede hacer cualquier cambio matemático en el proceso o usar funciones MySQL para modificar los valores.

 23
Author: Czechnology,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-25 11:30:55

Prueba esto:

update `list`
set `test` = `number`
 8
Author: juergen d,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-25 11:30:32

CUIDADO : El orden de las columnas de actualización es crítico

BUENO: Lo que quiero guarda el Valor existente de Status en PrevStatus

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

BAD: Status & PrevStatus ambos terminan como 44

UPDATE Collections SET  Status=44, PrevStatus=Status WHERE ID=1487496;
 4
Author: zzapper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-02 16:40:01

Intenta lo siguiente:

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

Crea una copia de todos los valores de "number" y péguelo en "test"

 1
Author: Jigneshsinh Rathod,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-27 06:53:56

Seguir funcionó para mí..

  1. Asegúrese de que no está utilizando el modo seguro en su aplicación de editor de consultas. Si, deshabilitar!
  2. Luego ejecute el siguiente comando sql

Para una tabla diga, 'test_update_cmd', columna de valor de origen col2, destino columna de valor col1 y columna de condición col3: -

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

¡Buena Suerte!

 1
Author: nitinr708,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-30 10:26:03

También puedes hacerlo con Procedimiento, así que tengo un procedimiento para esto

 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 ;
 -6
Author: Karan Kumar Mahto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-04 10:22:47