Cómo copiar datos de una tabla a otra nueva tabla en MySQL?


Quiero copiar datos de una tabla a otra en MySQL.

Cuadro 1 (cuadro existente):

aid    
st_id
from_uid
to_gid
to_uid
created
changed
subject
message
link

Cuadro 2 (Nuevo cuadro)

st_id
uid
changed
status
assign_status

Quiero copiar algunos campos de datos de la TABLA 1 en la TABLA 2.

¿Se puede hacer esto usando consultas MySQL?

 120
Author: Eric Leschinski, 2011-09-20

9 answers

Esto hará lo que quieras:

INSERT INTO table2 (st_id,uid,changed,status,assign_status)
SELECT st_id,from_uid,now(),'Pending','Assigned'
FROM table1

Si desea incluir todas las filas de la tabla1. De lo contrario, puede agregar una instrucción WHERE al final si desea agregar solo un subconjunto de tabla1.

Espero que esto ayude.

 222
Author: jdias,
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-30 21:36:58

Si no desea enumerar los campos, y la estructura de las tablas es la misma, puede hacer:

INSERT INTO `table2` SELECT * FROM `table1`;

O si desea crear una nueva tabla con la misma estructura:

CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;

Referencia para insert select; Referencia para crear tabla select

 52
Author: Bryan,
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-07-27 17:09:45

Puede obtener datos fácilmente de otra tabla. Tienes que añadir los campos que quieras.

La consulta mysql es:

INSERT INTO table_name1(fields you want)
  SELECT fields you want FROM table_name2


donde, los valores se copian de tabla2 a tabla1

 20
Author: php,
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-08-24 18:42:42

La mejor opción es usar INSERT...SELECCIONE sentencia en mysql.

Http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

 4
Author: dexter.ba,
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-09-20 09:14:42
SELECT *
INTO newtable [IN externaldb]
FROM table1;

Http://www.w3schools.com/sql/sql_select_into.asp

 3
Author: mikey,
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-01-11 19:29:15
CREATE TABLE newTable LIKE oldTable;

Luego, para copiar los datos sobre

INSERT INTO newTable SELECT * FROM oldTable;
 3
Author: Seymur Asadov,
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-05-23 21:08:50
INSERT INTO Table1(Column1,Column2..) SELECT Column1,Column2.. FROM Table2 [WHERE <condition>]
 2
Author: Nana Partykar,
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-11-12 15:15:17

Puedes probar este código

insert into #temp 
select Product_ID,Max(Grand_Total) AS 'Sales_Amt', Max(Rec_Amount) ,'',''
from Table_Name group by Id
 0
Author: Biddut,
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
2018-02-17 12:53:10

La consulta anterior solo funciona si hemos creado la tabla clients con columnas coincidentes del cliente

INSERT INTO clients(c_id,name,address)SELECT c_id,name,address FROM customer
 0
Author: Qanuni,
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
2018-02-17 14:27:13