0 votes
1 view
in SQL by (22.4k points)

I am new to MySQL. I would like to copy the content of one table to another table within the same database. Basically, I would like to insert a table from another table. Is there an easy way of doing this?

1 Answer

0 votes
by (40.3k points)

Try using the below query:

INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE;

But, if the tables have different structures then you can use this query:

INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;

To constrain, use this:

INSERT INTO TARGET_TABLE (`col1_`,`col2_`) SELECT `col1`,`col2` FROM SOURCE_TABLE WHERE `foo`=1

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...