The best SQL INSERT INTO SELECT statement Tutorial In 2024, In this tutorial you can learn SQL INSERT INTO SELECT statement,The demo database,SQL INSERT INTO SELECT Examples,Examples,Examples,
With SQL, you can copy information from one table to another.
INSERT INTO SELECT statement to copy data from a table and then insert data into an existing table.
INSERT INTO SELECT statement to copy data from a table and then insert data into an existing table. Target table any rows that already exist are not affected.
We can replicate all columns from a table into another existing table:
Or we can just copy the desired columns into another existing table:
In this tutorial, we will use w3big sample database.
The following is a selected "Websites" table data:
+----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | 本教程 | http://www.w3write.com/ | 4689 | CN | | 4 | 微博 | http://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND | +----+---------------+---------------------------+-------+---------+
Here is the "apps" APP data:
mysql> SELECT * FROM apps; +----+------------+-------------------------+---------+ | id | app_name | url | country | +----+------------+-------------------------+---------+ | 1 | QQ APP | http://im.qq.com/ | CN | | 2 | 微博 APP | http://weibo.com/ | CN | | 3 | 淘宝 APP | https://www.taobao.com/ | CN | +----+------------+-------------------------+---------+ 3 rows in set (0.00 sec)
Copy data "apps" are inserted into the "Websites" in:
QQ only complex of APP to "Websites" in: