insert into select
时间: 2023-08-03 14:08:01 浏览: 113
INSERT INTO SELECT is a SQL statement that allows you to insert rows into a table by selecting data from another table. It follows the syntax:
```
INSERT INTO table_name (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;
```
Here's an example to illustrate how it works:
Let's say we have two tables: `customers` and `new_customers`. We want to insert rows into the `customers` table by selecting data from the `new_customers` table.
```sql
INSERT INTO customers (customer_id, customer_name, email)
SELECT new_customer_id, new_customer_name, new_email
FROM new_customers
WHERE new_customer_status = 'active';
```
In this example, we are inserting rows into the `customers` table by selecting the `customer_id`, `customer_name`, and `email` columns from the `new_customers` table where the `new_customer_status` is 'active'.
Note that the column names and data types should match between the two tables for this operation to work correctly.
阅读全文