MySQL: ¿Cómo copiar filas, pero cambiar algunos campos?


Tengo un gran número de filas que me gustaría copiar, pero necesito cambiar un campo.

Puedo seleccionar las filas que quiero copiar:

select * from Table where Event_ID = "120"

Ahora quiero copiar todas esas filas y crear nuevas filas mientras establezco Event_ID a 155. ¿Cómo puedo lograr esto?

 154
Author: Andrew, 2010-05-06

6 answers

INSERT INTO Table
          ( Event_ID
          , col2
           ...
          )
     SELECT "155"
          , col2
           ...
      FROM Table WHERE Event_ID = "120"

Aquí, el col2,... representa las columnas restantes (las distintas de Event_ID) en tu tabla.

 210
Author: dcp,
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
2010-05-06 17:44:55

Esta es una solución en la que tiene muchos campos en su tabla y no quiere tener un calambre en el dedo al escribir todos los campos, simplemente escriba los necesarios:)

Cómo copiar algunas filas en la misma tabla, con algunos campos que tienen valores diferentes:

  1. Cree una tabla temporal con todas las filas que desea copiar
  2. Actualice todas las filas de la tabla temporal con los valores que desee
  3. Si tiene un campo de incremento automático, debe configurarlo en NULL en la tabla temporal
  4. Copie todas las filas de la tabla temporal en su tabla original
  5. Suprímase la tabla temporal

Su código:

CREATE table temporary_table AS SELECT * FROM original_table WHERE Event_ID="155";

UPDATE temporary_table SET Event_ID="120";

UPDATE temporary_table SET ID=NULL

INSERT INTO original_table SELECT * FROM temporary_table;

DROP TABLE temporary_table

Código de escenario general:

CREATE table temporary_table AS SELECT * FROM original_table WHERE <conditions>;

UPDATE temporary_table SET <fieldx>=<valuex>, <fieldy>=<valuey>, ...;

UPDATE temporary_table SET <auto_inc_field>=NULL;

INSERT INTO original_table SELECT * FROM temporary_table;

DROP TABLE temporary_table

Código simplificado / condensado:

CREATE TEMPORARY TABLE temporary_table AS SELECT * FROM original_table WHERE <conditions>;

UPDATE temporary_table SET <auto_inc_field>=NULL, <fieldx>=<valuex>, <fieldy>=<valuey>, ...;

INSERT INTO original_table SELECT * FROM temporary_table;

Como la creación de la tabla temporal utiliza la palabra clave TEMPORARY, se eliminará automáticamente cuando finalice la sesión (como sugiere @ar34z).

 115
Author: Alex Christodoulou,
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-10-08 21:50:11

Digamos que tu tabla tiene otras dos columnas: foo y bar

INSERT INTO Table (foo, bar, Event_ID)
SELECT foo, bar, "155"
  FROM Table
 WHERE Event_ID = "120"
 36
Author: Peter Bailey,
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
2010-05-06 17:40:11

Si tienes un montón de columnas en tu tabla y no quieres escribir cada una puedes hacerlo usando una tabla temporal, como;

SELECT *
INTO #Temp
FROM Table WHERE Event_ID = "120"
GO

UPDATE #TEMP
SET Column = "Changed"
GO

INSERT INTO Table
SELECT *
FROM #Temp
 10
Author: Davethebfg,
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
2011-12-21 11:26:56

Hey ¿Qué tal copiar todos los campos, cambiar uno de ellos al mismo valor + algo más.

INSERT INTO Table (foo, bar, Event_ID)
SELECT foo, bar, Event_ID+"155"
  FROM Table
 WHERE Event_ID = "120"

??????????

 2
Author: Dimitar,
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
2011-12-17 19:32:07

Mientras Event_ID sea Entero, haga esto:

INSERT INTO Table (foo, bar, Event_ID)
SELECT foo, bar, (Event_ID + 155)
  FROM Table
WHERE Event_ID = "120"
 0
Author: axel.becker,
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
2014-05-13 11:34:07