简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM