查看“︁MySQL/INSERT INTO”︁的源代码
来自康健生活
←
MySQL/INSERT INTO
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == 简化插入(省略字段,不推荐)。 {{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="mysql"> INSERT INTO user (name, email) VALUES ('lanlan', '');</syntaxhighlight>|<syntaxhighlight lang="bash"> Query OK, 1 row affected (0.01 sec) </syntaxhighlight>|mysql>}} 插入 null。 {{Shc_out|<syntaxhighlight lang="mysql"> INSERT INTO user (name, email) VALUES ('lanqian', NULL);</syntaxhighlight>|<syntaxhighlight lang="bash"> Query OK, 1 row affected (0.00 sec) </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"> INSERT INTO `md_user`(`name`, `datetime`) VALUES ('kangjian', '2021-12-29'); </syntaxhighlight> 插入多个值。 <syntaxhighlight lang="mysql"> INSERT INTO `md_user`(`name`, `datetime`) VALUES ('kangjian', '2021-12-29'), ('lilei', '2021-12-29'); </syntaxhighlight> 将搜索结果插入表中。 <syntaxhighlight lang="mysql"> 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>
此页面嵌入的页面:
模板:Shc out
(
查看源代码
)
模板:Shc out/style.css
(
查看源代码
)
返回
MySQL/INSERT INTO
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。