MySQL/INSERT INTO:修订间差异
来自康健生活
无编辑摘要 |
|||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
简化插入(省略字段,不推荐)。 | 简化插入(省略字段,不推荐)。 | ||
{{Shc_out|<syntaxhighlight lang=" | {{Shc_out|<syntaxhighlight lang="mysql"> | ||
INSERT INTO user VALUES (1, 'lan', '');</syntaxhighlight>|<syntaxhighlight lang="bash"> | INSERT INTO user VALUES (1, 'lan', '');</syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
Query OK, 1 row affected (0.01 sec) | Query OK, 1 row affected (0.01 sec) | ||
| 第7行: | 第7行: | ||
插入空值。 | 插入空值。 | ||
{{Shc_out|<syntaxhighlight lang=" | {{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) | ||
| 第13行: | 第13行: | ||
插入 null。 | 插入 null。 | ||
{{Shc_out|<syntaxhighlight lang=" | {{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) | ||
| 第20行: | 第20行: | ||
== 插入规则 == | == 插入规则 == | ||
=== 边查边插 === | === 边查边插 === | ||
{{Shc_out|<syntaxhighlight lang=" | {{Shc_out|<syntaxhighlight lang="mysql"> | ||
INSERT INTO table_1(field_1, field_2) | INSERT INTO table_1(field_1, field_2) | ||
SELECT field_3 as field_1, field_4 as field_2 FROM table_2 | SELECT field_3 as field_1, field_4 as field_2 FROM table_2 | ||
| 第27行: | 第27行: | ||
=== 多表关联边查边插 === | === 多表关联边查边插 === | ||
{{Shc_out|<syntaxhighlight lang=" | {{Shc_out|<syntaxhighlight lang="mysql"> | ||
INSERT INTO table_1(field_1, field_2) | 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 | 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 | LEFT JOIN table_3 as b ON a.filed = b.filed | ||
WHERE 1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash"> | WHERE 1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
</syntaxhighlight>| | </syntaxhighlight>|mysql>}} | ||
=== ignore === | === ignore === | ||
| 第137行: | 第137行: | ||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
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"> | 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>}} | </syntaxhighlight>|mysql>}} | ||
| 第175行: | 第184行: | ||
<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 | |||
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; | |||
</syntaxhighlight> | </syntaxhighlight> | ||