zoukankan      html  css  js  c++  java
  • mysql数据库中,通过一条insert into语句,同时插入多个值

    需求描述

      今天在看一本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

  • 相关阅读:
    uva10341
    android_定义多个Activity及跳转
    阿里巴巴2014年校园招聘(秋季招聘)在线笔试--測试研发project师
    关于程序猿的几个阶段!
    【Spring】Spring学习笔记-01-入门级实例
    感知器算法(二分类问题)
    Ubuntu14.04下安装ZendStudio10.6.1+SVN出现Failed to load JavaHL Library
    EF架构~关系表插入应该写在事务里,但不应该是分布式事务
    EF架构~在global.asax里写了一个异常跳转,不错!
    EF架构~为导航属性赋值时ToList()的替换方案
  • 原文地址:https://www.cnblogs.com/chuanzhang053/p/8617092.html
Copyright © 2011-2022 走看看