MySQL/INSERT INTO:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
小 Kangkk移动页面Mysql/INSERT至MySQL/INSERT INTO,不留重定向
 
(未显示同一用户的18个中间版本)
第1行: 第1行:
== 引言 ==
== 引言 ==
简化插入(省略字段,不推荐)。
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO user VALUES (1, 'lan', '');</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 1 row affected (0.01 sec)
</syntaxhighlight>|mysql>}}
插入空值。
插入空值。
{{Shc_out|<syntaxhighlight lang="bash">
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO user (name, email) VALUES ('lanlan', '');</syntaxhighlight>|<syntaxhighlight lang="bash">
INSERT INTO user (name, email) VALUES ('lanlan', '');</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
第7行: 第13行:


插入 null。
插入 null。
{{Shc_out|<syntaxhighlight lang="bash">
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO user (name, email) VALUES ('lanqian', NULL);</syntaxhighlight>|<syntaxhighlight lang="bash">
INSERT INTO user (name, email) VALUES ('lanqian', NULL);</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}


== 插入规则 ==
=== 边查边插 ===
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO table_1 (
    field_1,
    field_2
  )
SELECT
    field_3 AS field_1,
    field_4 as field_2
FROM
    table_2
WHERE
    1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|mysql>}}
=== 多表关联边查边插 ===
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO table_1 (
    field_1,
    field_2
  )
SELECT
    a.field_3 AS field_1,
    b.field_4 AS field_2
FROM
    table_2 AS a
LEFT JOIN
    table_3 as b
      ON a.filed = b.filed
WHERE 1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|mysql>}}
=== ignore ===
当数据存在(主键或唯一索引)时,忽略此次插入。
{{Shc_out|<syntaxhighlight lang="mysql">
SELECT * FROM user;</syntaxhighlight>|<syntaxhighlight lang="bash">
+----+----------+---------------------+
| id | name    | email              |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk  | kangkk@kangkk.com  |
|  3 | 365      | 365@kangkk.com      |
|  4 | test    | test@kangkk.com    |
|  5 | lanqian  | NULL                |
|  6 | lanlan  |                    |
+----+----------+---------------------+
6 rows in set (0.00 sec)
</syntaxhighlight>|mysql>}}
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT ignore INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'kk',
    'kk@kangkk.com'
  );</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 0 rows affected (0.00 sec)
</syntaxhighlight>|mysql>}}
{{Shc_out|<syntaxhighlight lang="mysql">
SELECT * FROM user;</syntaxhighlight>|<syntaxhighlight lang="bash">
+----+----------+---------------------+
| id | name    | email              |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk  | kangkk@kangkk.com  |
|  3 | 365      | 365@kangkk.com      |
|  4 | test    | test@kangkk.com    |
|  5 | lanqian  | NULL                |
|  6 | lanlan  |                    |
+----+----------+---------------------+
6 rows in set (0.00 sec)
</syntaxhighlight>|mysql>}}
=== on duplicate key update ===
如果数据(主键或唯一索引)存在,则执行更新操作。
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'kk',
    'kk@kangkk.com'
  )
ON DUPLICATE KEY UPDATE
    name = 'kk',
    email = 'kk@kangkk.com';</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 2 rows affected (0.01 sec)
</syntaxhighlight>|mysql>}}
{{Shc_out|<syntaxhighlight lang="mysql">
SELECT * FROM user;</syntaxhighlight>|<syntaxhighlight lang="bash">
+----+----------+---------------------+
| id | name    | email              |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk  | kangkk@kangkk.com  |
|  3 | kk      | kk@kangkk.com      |
|  4 | test    | test@kangkk.com    |
|  5 | lanqian  | NULL                |
|  6 | lanlan  |                    |
+----+----------+---------------------+
6 rows in set (0.00 sec)
</syntaxhighlight>|mysql>}}
=== replace into ===
替换插入。如果数据(主键或唯一索引)存在,则先删除再插入。
{{Shc_out|<syntaxhighlight lang="mysql">
REPLACE INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'lan',
    'lan@kangkk.com'
  );</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 2 rows affected (0.00 sec)
</syntaxhighlight>|mysql>}}
{{Shc_out|<syntaxhighlight lang="mysql">
SELECT * FROM user;</syntaxhighlight>|<syntaxhighlight lang="bash">
+----+----------+---------------------+
| id | name    | email              |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk  | kangkk@kangkk.com  |
|  3 | lan      | lan@kangkk.com      |
|  4 | test    | test@kangkk.com    |
|  5 | lanqian  | NULL                |
|  6 | lanlan  |                    |
+----+----------+---------------------+
6 rows in set (0.01 sec)
</syntaxhighlight>|mysql>}}
=== insert if not exists ===
如果数据不存在则插入,如果存在则忽略(不考虑主键和唯一索引)。
一个错误的示例。
{{Shc_out|<syntaxhighlight lang="mysql">
SELECT * FROM user;</syntaxhighlight>|<syntaxhighlight lang="bash">
+----+----------+---------------------+
| id | name    | email              |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk  | kangkk@kangkk.com  |
|  3 | lan      | lan@kangkk.com      |
|  4 | test    | test@kangkk.com    |
|  5 | lanqian  | NULL                |
|  6 | lanlan  |                    |
+----+----------+---------------------+
6 rows in set (0.01 sec)
</syntaxhighlight>|mysql>}}
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO user (
    name,
    email
  )
SELECT
    'kk',
    'kk@kangkk.com'
FROM
    user
WHERE not exists (
    SELECT name
    FROM user
    WHERE name='kk'
  );</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|mysql>}}
{{Shc_out|<syntaxhighlight lang="mysql">
SELECT * FROM user;</syntaxhighlight>|<syntaxhighlight lang="bash">
+----+----------+---------------------+
| id | name    | email              |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk  | kangkk@kangkk.com  |
|  3 | lan      | lan@kangkk.com      |
|  4 | test    | test@kangkk.com    |
|  5 | lanqian  | NULL                |
|  6 | lanlan  |                    |
|  9 | kk      | kk@kangkk.com      |
| 10 | kk      | kk@kangkk.com      |
| 11 | kk      | kk@kangkk.com      |
| 12 | kk      | kk@kangkk.com      |
| 13 | kk      | kk@kangkk.com      |
| 14 | kk      | kk@kangkk.com      |
+----+----------+---------------------+
12 rows in set (0.00 sec)
</syntaxhighlight>|mysql>}}
== other ==
插入单个值。
插入单个值。
<syntaxhighlight lang="mysql">
<syntaxhighlight lang="mysql">
第25行: 第235行:
<syntaxhighlight lang="mysql">
<syntaxhighlight lang="mysql">
INSERT INTO md_folder_user(role_id, folder_id, inviter_id, user_id)  
INSERT INTO md_folder_user(role_id, folder_id, inviter_id, user_id)  
select
SELECT
  tu.role_id as `role_id`,
    tu.role_id AS `role_id`,
  f.id as folder_id,
    f.id AS folder_id,
  NULL as inviter_id,
    NULL AS inviter_id,
  tu.user_id as user_id
    tu.user_id AS user_id
from md_folder as f  
FROM md_folder AS f  
left join md_team_user as tu on tu.team_id = f.team_id;
LEFT JOIN md_team_user AS tu ON tu.team_id = f.team_id;
</syntaxhighlight>
</syntaxhighlight>

2026年1月19日 (一) 13:41的最新版本

引言

简化插入(省略字段,不推荐)。

mysql>
INSERT INTO user VALUES (1, 'lan', '');
Query OK, 1 row affected (0.01 sec)

插入空值。

mysql>
INSERT INTO user (name, email) VALUES ('lanlan', '');
Query OK, 1 row affected (0.01 sec)

插入 null。

mysql>
INSERT INTO user (name, email) VALUES ('lanqian', NULL);
Query OK, 1 row affected (0.00 sec)

插入规则

边查边插

mysql>
INSERT INTO table_1 (
    field_1,
    field_2
  )
SELECT
    field_3 AS field_1,
    field_4 as field_2
FROM
    table_2 
WHERE
    1 = 1;

多表关联边查边插

mysql>
INSERT INTO table_1 (
    field_1,
    field_2
  )
SELECT
    a.field_3 AS field_1,
    b.field_4 AS field_2
FROM
    table_2 AS a
LEFT JOIN
    table_3 as b
      ON a.filed = b.filed
WHERE 1 = 1;

ignore

当数据存在(主键或唯一索引)时,忽略此次插入。

mysql>
SELECT * FROM user;
+----+----------+---------------------+
| id | name     | email               |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk   | kangkk@kangkk.com   |
|  3 | 365      | 365@kangkk.com      |
|  4 | test     | test@kangkk.com     |
|  5 | lanqian  | NULL                |
|  6 | lanlan   |                     |
+----+----------+---------------------+
6 rows in set (0.00 sec)
mysql>
INSERT ignore INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'kk',
    'kk@kangkk.com'
  );
Query OK, 0 rows affected (0.00 sec)
mysql>
SELECT * FROM user;
+----+----------+---------------------+
| id | name     | email               |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk   | kangkk@kangkk.com   |
|  3 | 365      | 365@kangkk.com      |
|  4 | test     | test@kangkk.com     |
|  5 | lanqian  | NULL                |
|  6 | lanlan   |                     |
+----+----------+---------------------+
6 rows in set (0.00 sec)

on duplicate key update

如果数据(主键或唯一索引)存在,则执行更新操作。

mysql>
INSERT INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'kk',
    'kk@kangkk.com'
  )
ON DUPLICATE KEY UPDATE
    name = 'kk',
    email = 'kk@kangkk.com';
Query OK, 2 rows affected (0.01 sec)
mysql>
SELECT * FROM user;
+----+----------+---------------------+
| id | name     | email               |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk   | kangkk@kangkk.com   |
|  3 | kk       | kk@kangkk.com       |
|  4 | test     | test@kangkk.com     |
|  5 | lanqian  | NULL                |
|  6 | lanlan   |                     |
+----+----------+---------------------+
6 rows in set (0.00 sec)

replace into

替换插入。如果数据(主键或唯一索引)存在,则先删除再插入。

mysql>
REPLACE INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'lan',
    'lan@kangkk.com'
  );
Query OK, 2 rows affected (0.00 sec)
mysql>
SELECT * FROM user;
+----+----------+---------------------+
| id | name     | email               |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk   | kangkk@kangkk.com   |
|  3 | lan      | lan@kangkk.com      |
|  4 | test     | test@kangkk.com     |
|  5 | lanqian  | NULL                |
|  6 | lanlan   |                     |
+----+----------+---------------------+
6 rows in set (0.01 sec)

insert if not exists

如果数据不存在则插入,如果存在则忽略(不考虑主键和唯一索引)。

一个错误的示例。

mysql>
SELECT * FROM user;
+----+----------+---------------------+
| id | name     | email               |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk   | kangkk@kangkk.com   |
|  3 | lan      | lan@kangkk.com      |
|  4 | test     | test@kangkk.com     |
|  5 | lanqian  | NULL                |
|  6 | lanlan   |                     |
+----+----------+---------------------+
6 rows in set (0.01 sec)
mysql>
INSERT INTO user (
    name,
    email
  )
SELECT
    'kk',
    'kk@kangkk.com'
FROM
    user
WHERE not exists (
    SELECT name
    FROM user
    WHERE name='kk'
  );
mysql>
SELECT * FROM user;
+----+----------+---------------------+
| id | name     | email               |
+----+----------+---------------------+
|  1 | kangjian | kangjian@kangkk.com |
|  2 | kangkk   | kangkk@kangkk.com   |
|  3 | lan      | lan@kangkk.com      |
|  4 | test     | test@kangkk.com     |
|  5 | lanqian  | NULL                |
|  6 | lanlan   |                     |
|  9 | kk       | kk@kangkk.com       |
| 10 | kk       | kk@kangkk.com       |
| 11 | kk       | kk@kangkk.com       |
| 12 | kk       | kk@kangkk.com       |
| 13 | kk       | kk@kangkk.com       |
| 14 | kk       | kk@kangkk.com       |
+----+----------+---------------------+
12 rows in set (0.00 sec)

other

插入单个值。

INSERT INTO `md_user`(`name`, `datetime`) VALUES ('kangjian', '2021-12-29');

插入多个值。

INSERT INTO `md_user`(`name`, `datetime`) VALUES ('kangjian', '2021-12-29'), ('lilei', '2021-12-29');

将搜索结果插入表中。

INSERT INTO md_folder_user(role_id, folder_id, inviter_id, user_id) 
SELECT
    tu.role_id AS `role_id`,
    f.id AS folder_id,
    NULL AS inviter_id,
    tu.user_id AS user_id
FROM md_folder AS f 
LEFT JOIN md_team_user AS tu ON tu.team_id = f.team_id;