> ## Documentation Index
> Fetch the complete documentation index at: https://villagesql.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MySQL INSERT SELECT

> INSERT INTO ... SELECT 如何把查询的行复制到 MySQL 表中，列如何对应，以及列不对应时 ERROR 1136 在告诉您什么。

> [card] [VillageSQL is a drop-in replacement for MySQL with extensions.](https://villagesql.com/docs/en/8.4/stable/install-server.md)
>
> All examples on this page work on VillageSQL. Install Now →

`VALUES` 提供您输入的行。放在它位置上的 `SELECT` 提供服务器算出的行，所以能查询出来的汇总就是能存储起来的汇总。报表表就是这样填充的，行也是这样从一个表移到另一个表的。

这些示例使用 [INSERT](https://villagesql.com/docs/zh/tutorial/insert.md) 中的 `sakila_practice` 数据库。开始之前，请再运行一次那里的设置代码块，然后用 `mysql -u root -p sakila_practice` 打开客户端。

## INSERT SELECT 的工作原理

```sql theme={null}
INSERT INTO TARGET_TABLE (COLUMN_LIST)
SELECT SOURCE_COLUMNS FROM SOURCE_TABLE;
```

把 `TARGET_TABLE` 替换为被写入的表，`COLUMN_LIST` 替换为它的列，`SOURCE_COLUMNS` 替换为查询返回的内容，`SOURCE_TABLE` 替换为被读取的表。

这里没有 `VALUES` 关键字。列按位置配对，所以 `SELECT` 返回的第一个东西进入列表中的第一列，无论这 2 个叫什么名字。查询返回的每一行都成为目标表中的一行。

## 示例

### 存储一份汇总

`rating_count` 是空的，而 `sakila.film` 保存着答案。用 `database.table` 的形式指定表会从另一个数据库读取它，所以 1 条语句可以跨越这 2 个数据库。

```sql theme={null}
INSERT INTO rating_count (rating, films)
SELECT rating, COUNT(*) FROM sakila.film GROUP BY rating;
```

```text theme={null}
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0
```

`GROUP BY` 产生了 5 行，写入了 5 行。

```sql theme={null}
SELECT * FROM rating_count ORDER BY films DESC;
```

```text theme={null}
+--------+-------+
| rating | films |
+--------+-------+
| PG-13  |   223 |
| NC-17  |   210 |
| R      |   195 |
| PG     |   194 |
| G      |   178 |
+--------+-------+
5 rows in set (0.00 sec)
```

这些现在是普通的行，而且是一份快照。`film` 变化时，没有任何东西会更新它们。

### 在写入途中构造值

`SELECT` 可以计算任何东西，所以写入的行不必以那种形态存在于任何地方。

```sql theme={null}
INSERT INTO member (first_name, last_name, email, joined)
SELECT first_name, last_name, CONCAT(LOWER(first_name), '.', LOWER(last_name), '@example.net'), '2026-05-01'
FROM sakila.staff;
```

```text theme={null}
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
```

```sql theme={null}
SELECT member_id, first_name, last_name, email FROM member WHERE joined = '2026-05-01';
```

```text theme={null}
+-----------+------------+-----------+--------------------------+
| member_id | first_name | last_name | email                    |
+-----------+------------+-----------+--------------------------+
|         4 | Mike       | Hillyer   | mike.hillyer@example.net |
|         5 | Jon        | Stephens  | jon.stephens@example.net |
+-----------+------------+-----------+--------------------------+
2 rows in set (0.00 sec)
```

地址列由 `CONCAT` 生成，日期是在每一行上重复的常量。

## ERROR 1136 表示 2 个列表长度不同

```sql theme={null}
INSERT INTO rating_count (rating, films)
SELECT rating FROM sakila.film GROUP BY rating;
```

```text theme={null}
ERROR 1136 (21S01): Column count doesn't match value count at row 1
```

指定了 2 列，返回了 1 列。出现这个错误时，2 个列表的长度总是不同，所以请数一数。没有任何东西检查名称或含义，所以一个返回了正确数量的错误列的 `SELECT` 会被接受，并写入无意义的内容。请先单独运行 `SELECT`，读一读返回的内容。

## 第二次运行会遇上表的键

查询返回的每一行都会被提供给表，而表的规则仍然适用。

```sql theme={null}
INSERT INTO rating_count (rating, films)
SELECT rating, COUNT(*) FROM sakila.film WHERE rating = 'G' GROUP BY rating;
```

```text theme={null}
ERROR 1062 (23000): Duplicate entry 'G' for key 'rating_count.PRIMARY'
```

`rating` 是主键，而 `G` 已经存储了。想要刷新而不是只建立一次的汇总需要 [upsert](https://villagesql.com/docs/zh/tutorial/upsert.md)，它会更新已存储的行而不是失败。

## 小结

* 把 `SELECT` 放在 `VALUES` 的位置，不写 `VALUES` 关键字。
* 列按位置配对，从不按名称。
* 把 `ERROR 1136` 理解为 2 个长度不同的列表。
* 用 `database.table` 指定表，在同一条语句中从另一个数据库读取。
* 第二次运行会撞上目标表的键，与任何其他插入一样。

## 另请参阅

* [INSERT](https://villagesql.com/docs/zh/tutorial/insert.md) — 接受输入值的形式
* [插入多行](https://villagesql.com/docs/zh/tutorial/insert-multiple-rows.md) — 不用查询插入多行
* [Upsert](https://villagesql.com/docs/zh/tutorial/upsert.md) — 刷新第二次运行会与之冲突的行
* [MySQL 中的批量插入](https://villagesql.com/docs/zh/guides/bulk-inserts.md) — 移动大量数据，以及它的代价


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.