teraspora
teraspora

Reputation: 442

MySql: How do I insert multiple columns from multiple rows into another table?

INSERT INTO destination_table (source_id, foo_id, bar) 
VALUES( 
    (SELECT id, foo_id, bar FROM source_table WHERE id=399274),
    (SELECT id, foo_id, bar FROM source_table WHERE id=399279),
    (SELECT id, foo_id, bar FROM source_table WHERE id=380409), 
    (SELECT id, foo_id, bar FROM source_table WHERE id=400037), 
    (SELECT id, foo_id, bar FROM source_table WHERE id=401026)
);

but this results in an error: ERROR 1241 (21000): Operand should contain 1 column(s)

Upvotes: 1

Views: 576

Answers (1)

ysth
ysth

Reputation: 98388

Instead of the insert...values syntax, use insert...select:

INSERT INTO destination_table (source_id, foo_id, bar) 
SELECT id, foo_id, bar FROM source_table
WHERE id in (399274,399279,380409,400037,401026);

Upvotes: 2

Related Questions