MySQL ¿Cómo SE INSERTA EN una tabla con una subconsulta SELECT devolviendo varias filas?


MySQL ¿Cómo SE INSERTA EN una tabla con una subconsulta SELECT devolviendo varias filas?

  INSERT INTO Results
    (
     People,
     names,
    )
    VALUES
    (
     (
       SELECT d.id
       FROM Names f
       JOIN People d ON d.id  = f.id
     ),

     (
      "Henry"
     ),
    );

Quiero rellenar la nueva tabla con todos los resultados de esta subconsulta. Cómo hago esto sin obtener un ERROR 1242 (21000): Subconsulta devuelve más de 1 fila

Author: stackoverflow, 2012-02-24

7 answers

INSERT INTO Results (People, names )
   SELECT d.id, 'Henry'
   FROM Names f
   JOIN People d ON d.id  = f.id

Combine la cadena estática Henry con su consulta SELECT.

 95
Author: Ryan,
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
2012-02-23 22:40:10
INSERT INTO Results
    (
     People,
     names,
    )
    VALUES
    (
     (
       SELECT d.id
       FROM Names f
       JOIN People d ON (d.id  = f.id) limit 1
     ),

     (
      "Henry"
     ),
    );
 9
Author: Umar Enesi Ibrahim,
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
2012-11-08 12:27:44

Esto es lo que he encontrado que funciona bien. Es un poco largo, pero muchas veces los datos adicionales necesitan ser barajados.

Inserte varias filas en tabla1 desde tabla2 con valores. EJEMPLOS:

INSERT INTO table1 (col1, col2, col3, col4, col5) 
SELECT col1,col2,col3,col4,col5 
FROM table2 t2 
WHERE t2.val2 IN (MULTIPLE VALUES) 
AND (Another Conditional);

Puede insertar valores codificados para obtener insertar varias filas con datos repetidos:

INSERT INTO table1 (col1, col2, col3, col4, col5) 
SELECT "Value", col2, col3, "1900-01-01","9999-12-31" 
FROM table2 t2 
WHERE t2.val2 IN (MULTIPLE VALUES) 
AND (Another Conditional);

Tenga en cuenta que: "Value","1900-01-01","9999-12-31" se repetirá en todas las filas insertadas.

 6
Author: MiggityMac,
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-04-07 15:41:29
  INSERT INTO Results
    (
     People,
     names,
    )
    SELECT d.id, 'Henry'
    FROM Names f
    JOIN People d ON d.id  = f.id
 5
Author: triclosan,
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
2012-02-24 16:25:59

La razón de este error (subconsulta devuelve más de 1 fila) es que utiliza paréntesis (). Mira con más cuidado a la mejor respuesta. No contiene parética alrededor de subconsulta

 1
Author: Sveteek,
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-05-07 09:14:13

En MySQL se pueden insertar múltiples valores de cadenas como los siguientes evitando duplicados. Gracias.

   insert into brand(name) select * from ( 
select 'Fender' as name 
union select 'a' 
union ..... ) t 
where not exists (select 1 from brand t2 where t2.name COLLATE latin1_general_ci = t.name COLLATE utf8mb4_unicode_ci )
 0
Author: Lorenzo,
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-06-10 17:07:42

Inserte en ec_element (parentid,name) seleccione elementid,' STARTUP 'desde ec_element donde name = 'BG';

La instrucción Insert toma los valores elementid de la tabla que se encuentra con la condición cumplida y una cadena de etiqueta.

 -1
Author: sahmad,
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-01-31 11:18:12