mysql - > insertar en tbl (seleccionar de otra tabla) y algunos valores predeterminados


Como dice el título, estoy tratando de insertar en una tabla seleccionando valores de otra tabla y algunos valores predeterminados.

INSERT INTO def (catid, title, page, publish) 
(SELECT catid, title from abc),'page','yes')


INSERT INTO def (catid, title, page, publish) 
VALUES
((SELECT catid, title from abc),'page','yes'))

La primera consulta da un error de mysql y la segunda da el recuento de columnas no coincide.

¿Qué debo hacer?

Author: SQB, 2011-05-06

5 answers

Simplemente tienes que hacer:

INSERT INTO def (catid, title, page, publish) 
SELECT catid, title, 'page','yes' from `abc`
 201
Author: Nican,
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-05-06 05:38:44

Si desea copiar un subconjunto de la tabla de origen, puede hacer:

INSERT INTO def (field_1, field_2, field3)

SELECT other_field_1, other_field_2, other_field_3 from `abc`

O para copiar TODOS los campos de la tabla de origen a la tabla de destino puede hacer más simplemente:

INSERT INTO def 
SELECT * from `abc`
 6
Author: Redips77,
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-24 15:59:44

Si desea insertar todas las columnas,

insert into def select * from abc;

Aquí el número de columnas en def debe ser igual a abc.

Si desea insertar los subconjuntos de columnas,

insert into def (col1,col2, col3 ) select scol1,scol2,scol3 from abc; 

Si desea insertar algunos valores hardcorded entonces

insert into def (col1, col2,col3) select 'hardcoded value',scol2, scol3 from abc;
 4
Author: kanishka vatsa,
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
2016-09-08 06:57:57
INSERT INTO def (field_1, field_2, field3) 
VALUES 
('$field_1', (SELECT id_user from user_table where name = 'jhon'), '$field3')
 2
Author: Frankie,
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-20 18:13:11

Con MySQL si está insertando en una tabla que tiene una clave primaria de incremento automático y desea usar una función MySQL incorporada como NOW(), entonces puede hacer algo como esto:

INSERT INTO course_payment 
SELECT NULL, order_id, payment_gateway, total_amt, charge_amt, refund_amt, NOW()
FROM orders ORDER BY order_id DESC LIMIT 10;
 2
Author: crmpicco,
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-23 11:10:39