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?
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.
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
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
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.
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;
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;
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>]
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
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
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