mxlfa
mxlfa

Reputation: 95

Multiple Select statements into an Insert statement

I've been searching for a while and I'm not sure if this is allowed in mysql.

I know this is allowed in mysql

Insert into 'table' (column1, column2, column3) Select val1, val2, val3 From sometable

Also this

Insert into 'table' (column1, column2, column3) Values (val1, val2, val3), (val1, val2, val3)

I am not sure if this is allowed though:

Insert into 'table' (column1, column2, column3) Select val1, val2, val3 From sometable, Select val1, val2, val3 From sometable

Obviously that gave me an error, is that allowed in mysql?

Upvotes: 0

Views: 36

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270993

You can try it, and it will fail. Use union all instead of ,:

Insert into table (column1, column2, column3)
    Select val1, val2, val3 From sometable
    union all
    Select val1, val2, val3 From sometable;

I assume the single quotes were there for some sort of effect, because you say that the first two queries actually work.

Upvotes: 2

Related Questions