Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k 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.7k points)
edited by

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

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 18, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...