SQL Getting Sta...login
SQL Getting Started Tutorial Manual
author:php.cn  update time:2022-04-12 14:15:40

SQL SELECT INTO



With SQL, you can copy information from one table to another.

The SELECT INTO statement copies data from one table and then inserts the data into a new table.


SQL SELECT INTO statement

The SELECT INTO statement copies data from one table and then inserts the data into another new table.

SQL SELECT INTO syntax

We can copy all columns and insert them into a new table:

//SELECT *
//INTO newtable [IN externaldb]
//FROM table1;

Or just copy the desired columns and insert them into the new table:

SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;

lamp

## Tips: The new table will use the SELECT statement Create the column names and types defined in . You can use the AS clause to apply the new name.


##SQL SELECT INTO Example

Create a backup copy of Customers:

SELECT *
INTO WebsitesBackup2016
FROM Websites;

Please use the IN clause to copy the table to another database:

SELECT *
INTO WebsitesBackup2016 IN 'Backup.mdb'
FROM Websites;

Copy only some columns and insert them into the new table:

SELECT name, url
INTO WebsitesBackup2016
FROM Websites;

Copy only Chinese websites and insert them into the new table:

SELECT *
INTO WebsitesBackup2016
FROM Websites
WHERE country='CN';

Copy data from multiple tables and insert it into a new table:

            SELECT Websites.name, access_log.count, access_log.date
INTO WebsitesBackup2016
FROM Websites
LEFT JOIN access_log
ON Websites.id=access_log.site_id;

Tip:

The SELECT INTO statement can be used to create a new empty table through another schema. Just add a WHERE clause that causes the query to return no data:

          SELECT *
INTO
newtable
FROM table1
WHERE 1=0;