mysql数据库中,通过一条insert into语句,同时插入多个值
2023-09-11 14:14:16 时间
需求描述:
今天在看一本mysql的书籍,发现一个mysql中insert into好用的技巧,就是通过
1条insert into语句,插入多行数据,而不是多个insert into语句。在此记录下。
测试过程:
1.常规的通过多个insert into语句插入多行数据
create table tab_ts01(id int,num01 int); insert into tab_ts01 values (1,2); insert into tab_ts01 values (2,3); insert into tab_ts01 values (5,55); insert into tab_ts01 values (40,22);
执行过程:
mysql> drop table if exists tab_ts01; Query OK, 0 rows affected (0.08 sec) mysql> create table tab_ts01(id int,num01 int); Query OK, 0 rows affected (0.03 sec) mysql> insert into tab_ts01 values (1,2); Query OK, 1 row affected (0.02 sec) mysql> insert into tab_ts01 values (2,3); Query OK, 1 row affected (0.01 sec) mysql> insert into tab_ts01 values (5,55); Query OK, 1 row affected (0.01 sec) mysql> insert into tab_ts01 values (40,22); Query OK, 1 row affected (0.01 sec) mysql> select * from tab_ts01; +------+-------+ | id | num01 | +------+-------+ | 1 | 2 | | 2 | 3 | | 5 | 55 | | 40 | 22 | +------+-------+ 4 rows in set (0.00 sec)
2.通过一条insert into语句,插入多行值
drop table if exists tab_ts01; create table tab_ts01(id int,num01 int); insert into tab_ts01 values (1,2),(2,3),(5,55),(40,22);
执行过程:
mysql> drop table if exists tab_ts01; Query OK, 0 rows affected (0.01 sec) mysql> create table tab_ts01(id int,num01 int); Query OK, 0 rows affected (0.12 sec) mysql> insert into tab_ts01 values (1,2),(2,3),(5,55),(40,22); Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from tab_ts01; +------+-------+ | id | num01 | +------+-------+ | 1 | 2 | | 2 | 3 | | 5 | 55 | | 40 | 22 | +------+-------+ 4 rows in set (0.00 sec)
备注:发现通过一条insert into语句能够达到与多个insert into语句同样的效果,而且更加的方便,可以作为一个小技巧。
文档创建时间:2018年3月21日15:02:39
相关文章
- 【MySQL】批量删除mysql中数据库中的表
- MYSQL数据库优化
- MySql通过数据库文件恢复数据库
- mysql分享一:运维角度浅谈MySQL数据库优化
- 修改mysql/MariaDB数据库的端口号+远程
- MYSQL随机抽取查询 MySQL Order By Rand()效率问题
- mysql基础使用
- 使用mysql-connector-python操作MYSQL数据库
- 数据库基础之Mysql(3)mysql删除历史binlog
- MYSQL数据库的导出的几种方法
- Atitit postgresql data type 数据类型与mysql对应表 数据库常用数据类型 PostgreMysql 整数intgreterInt 小数numericFL
- mysql数据库(2):输入查询
- 项目中常用的19条MySQL优化
- Python:mysql-connector-python模块对MySQL数据库进行增删改查
- 详解MySQL information_schema数据库常用的表信息以及各表对应的字段信息;以及如何登录mysql和创建视图
- 上Mysql com.mysql.jdbc.StatementImpl$CancelTask内存泄漏问题和解决方法
- mysql-connector-java与Mysql、Java的对应版本
- MySQL数据库下.frm .MYD .MYI损坏恢复操作——筑梦之路
- Mysql 问题集
- mysql数据库表分区详解(数量过大的数据库表通过分区提高查询速度)
- MySQL----MySQL将数据从一个数据导入到另一个数据库
- Mysql报错:Can't connect to local MySQL server through socket '/tmp/mysql.sock'
- 【MySQL】mysql查询语句大总结_Unit04
- 【MySql】MySQL数据库--什么是MySQL的回表 ?