MySQL/INSERT INTO:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
小 Kangkk移动页面Mysql/INSERT至MySQL/INSERT INTO,不留重定向
 
(未显示同一用户的9个中间版本)
第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)
第13行: 第19行:


== 插入规则 ==
== 插入规则 ==
=== 边查边插 ===
{{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 ===
=== ignore ===
当数据存在(主键或唯一索引)时,忽略此次插入。
当数据存在(主键或唯一索引)时,忽略此次插入。
第31行: 第69行:


{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT ignore INTO user(id, name, email) values(3, 'kk', 'kk@kangkk.com');</syntaxhighlight>|<syntaxhighlight lang="bash">
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)
Query OK, 0 rows affected (0.00 sec)
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}
第53行: 第100行:
如果数据(主键或唯一索引)存在,则执行更新操作。
如果数据(主键或唯一索引)存在,则执行更新操作。
{{Shc_out|<syntaxhighlight lang="mysql">
{{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">
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)
Query OK, 2 rows affected (0.01 sec)
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}
第76行: 第135行:


{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
REPLACE INTO user(id, name, email) values(3, 'lan', 'lan@kangkk.com');</syntaxhighlight>|<syntaxhighlight lang="bash">
REPLACE INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'lan',
    'lan@kangkk.com'
  );</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 2 rows affected (0.00 sec)
Query OK, 2 rows affected (0.00 sec)
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}
第93行: 第161行:
+----+----------+---------------------+
+----+----------+---------------------+
6 rows in set (0.01 sec)
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>}}
</syntaxhighlight>|mysql>}}


第109行: 第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>