INSERTAR EN SELECT SELECCIONAR para todas las columnas de MySQL


Estoy tratando de mover datos antiguos de:

this_table >> this_table_archive

Copiando todas las columnas. He intentado esto, pero no funciona:

INSERT INTO this_table_archive (*) VALUES (SELECT * FROM this_table WHERE entry_date < '2011-01-01 00:00:00');

Nota: las tablas son idénticas y tienen id como clave primaria.

Author: shgnInc, 2011-03-10

4 answers

La sintaxis correcta se describe en el manual . Prueba esto:

INSERT INTO this_table_archive (col1, col2, ..., coln)
SELECT col1, col2, ..., coln
FROM this_table
WHERE entry_date < '2011-01-01 00:00:00';

Si las columnas id son una columna de incremento automático y ya tiene algunos datos en ambas tablas, en algunos casos puede omitir el id de la lista de columnas y generar nuevos ID para evitar insertar un id que ya existe en la tabla original. Si su tabla de destino está vacía, esto no será un problema.

 178
Author: Mark Byers,
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-03-09 22:56:42

Para la sintaxis, se ve así (omita la lista de columnas para significar implícitamente "todo")

INSERT INTO this_table_archive
SELECT *
FROM this_table
WHERE entry_date < '2011-01-01 00:00:00'

Para evitar errores de clave primaria si ya tiene datos en la tabla de archivo

INSERT INTO this_table_archive
SELECT t.*
FROM this_table t
LEFT JOIN this_table_archive a on a.id=t.id
WHERE t.entry_date < '2011-01-01 00:00:00'
  AND a.id is null  # does not yet exist in archive
 57
Author: RichardTheKiwi,
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-07-30 16:00:12

Adición a Mark Byers respuesta:

A veces también desea insertar Detalles codificados de lo contrario puede haber un fallo de restricción único, etc. Por lo tanto, use el siguiente en una situación en la que anule algunos valores de las columnas.

INSERT INTO matrimony_domain_details (domain, type, logo_path)
SELECT 'www.example.com', type, logo_path
FROM matrimony_domain_details
WHERE id = 367

Aquí dominio el valor es agregado por mí me en forma codificada para deshacerse de la restricción única.

 18
Author: Pratik C Joshi,
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-08-14 07:44:33

¿No necesita double () para el bit de valores? si no probar esto (aunque debe haber una mejor manera

insert into this_table_archive (id, field_1, field_2, field_3) 
values
((select id from this_table where entry_date < '2001-01-01'), 
((select field_1 from this_table where entry_date < '2001-01-01'), 
((select field_2 from this_table where entry_date < '2001-01-01'), 
((select field_3 from this_table where entry_date < '2001-01-01'));
 4
Author: Daniel Casserly,
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
2013-05-14 22:29:14