Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert multiple rows from other table - "Subquery returns more than 1 row"

I table 1 contains:

    |col1|
    | 1  |
    | 2  |
    | 1  |
    | 3  |
    | 1  |
    | 2  |
    | 4  |
    | 2  |
    | 3  |
    | 1  |

and I have another table has a column name val, and my code is

INSERT INTO table2(value) VALUES ((select distinct col1 from table1))

I got the #1242 - Subquery returns more than 1 row.
How can I get multiple rows insert into my table2 ?

like image 676
user3552387 Avatar asked Nov 25 '25 15:11

user3552387


1 Answers

You don't need the values statement when using insert . . . select:

INSERT INTO table2(value)
    select distinct col1
    from table1;
like image 84
Gordon Linoff Avatar answered Nov 27 '25 11:11

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!