MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows?

stackoverflow picture stackoverflow · Feb 23, 2012 · Viewed 114.2k times · Source

MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows?

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

     (
      "Henry"
     ),
    );

I WANT to populate the new table with all results returning from this subquery. How do I do this without getting a ERROR 1242 (21000): Subquery returns more than 1 row

Answer

Ryan picture Ryan · Feb 23, 2012
INSERT INTO Results (People, names )
   SELECT d.id, 'Henry'
   FROM Names f
   JOIN People d ON d.id  = f.id

Combine the static string Henry with your SELECT query.