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?
7 answers
UPDATE `table` SET test=number
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.
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`
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;
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"
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í..
- Asegúrese de que no está utilizando el modo seguro en su aplicación de editor de consultas. Si, deshabilitar!
- 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!
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 ;
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