MySQL:使用INSERT 插入多條記錄
呼叫多次INSERT語句也可以插入多條記錄,但使用這種方法要增加伺服器的負荷,因為執行每一次SQL,伺服器都要同樣對SQL進行分析、最佳化等操作。
MySQL提供了另一種解決方案,就是使用一條INSERT語句來插入多條記錄。這並不是標準的SQL語法,因此只能在MySQL中使用。
示例:
INSERT INTO users(name, age) VALUES('姚明', 25), ('比爾.蓋茨', 50), ('火星人',600);
上面的INSERT語句向users表中連續插入了3條記錄。值得注意的是,上面的INSERT語句中的VALUES後必須每一條記錄的值放到一對(…)中,中間使用","分割。
INSERT INTO table1(i) VALUES(1),(2),(3),(4),(5);
當然,這種寫法也可以省略列名,這樣每一對括號裡的值的數目必須一致,而且這個數目必須和列數一致。如:
INSERT INTO table1 VALUES(1),(2),(3),(4),(5);
驗證:可行
[root@CentOS6 ~]# date
Tue Dec 31 09:40:09 CST 2013
[root@CentOS6 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.71 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Test |
| mysql |
+--------------------+
3 rows in set (0.06 sec)
mysql> use Test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_Test |
+----------------+
| DB |
+----------------+
1 row in set (0.00 sec)
mysql> select * from DB;
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | 張三 | 23 | 男 |
| 2 | 李四 | 25 | 男 |
| 3 | 王五 | 24 | 男 |
| 4 | 鄭七 | 22 | 女 |
| 5 | 梁九 | 21 | 女 |
| 6 | Tom | 24 | 男 |
| 7 | Jim | 24 | 男 |
| 8 | Lucy | 22 | 女 |
| 9 | John | 23 | 男 |
+----+--------+-----+--------+
9 rows in set (0.00 sec)
mysql> insert into DB(id,name,age,gender) values('10','劉三','24','男'),('11','李六','25','男'),('12','Eric','27','男');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from DB;
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | 張三 | 23 | 男 |
| 2 | 李四 | 25 | 男 |
| 3 | 王五 | 24 | 男 |
| 4 | 鄭七 | 22 | 女 |
| 5 | 梁九 | 21 | 女 |
| 6 | Tom | 24 | 男 |
| 7 | Jim | 24 | 男 |
| 8 | Lucy | 22 | 女 |
| 9 | John | 23 | 男 |
| 10 | 劉三 | 24 | 男 |
| 11 | 李六 | 25 | 男 |
| 12 | Eric | 27 | 男 |
+----+--------+-----+--------+
12 rows in set (0.00 sec)
mysql> insert into DB values('13','劉七','24','女'),('14','魯六','21','男'),('15','Lily','22','女');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from DB;
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | 張三 | 23 | 男 |
| 2 | 李四 | 25 | 男 |
| 3 | 王五 | 24 | 男 |
| 4 | 鄭七 | 22 | 女 |
| 5 | 梁九 | 21 | 女 |
| 6 | Tom | 24 | 男 |
| 7 | Jim | 24 | 男 |
| 8 | Lucy | 22 | 女 |
| 9 | John | 23 | 男 |
| 10 | 劉三 | 24 | 男 |
| 11 | 李六 | 25 | 男 |
| 12 | Eric | 27 | 男 |
| 13 | 劉七 | 24 | 女 |
| 14 | 魯六 | 21 | 男 |
| 15 | Lily | 22 | 女 |
+----+--------+-----+--------+
15 rows in set (0.00 sec)
mysql>
原釋出於
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29734436/viewspace-1247999/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL防止重複插入相同記錄 insert if not existsMySql
- MySQL INSERT插入條件判斷:如果不存在則插入MySql
- Oracle 使用一條insert語句完成多表插入Oracle
- 【SQL】使用一條INSERT語句完成多表插入SQL
- 用一條mysql語句插入多條資料MySql
- mysql怎麼插入空記錄MySql
- SQL INSERT INTO 語句詳解:插入新記錄、多行插入和自增欄位SQL
- mysql初始化表資料及插入多條資料學習筆記MySql筆記
- 【INSERT】在INSERT插入語句中引入條件限制選項實現資料插入控制
- mysql語句:批量更新多條記錄的不同值[轉]MySql
- oracle單條sql與plsql rowid插入記錄小記OracleSQL
- mysql刪除一條記錄MySql
- MySQL 當記錄不存在時插入,當記錄存在時更新MySql
- [Q]怎麼實現一條記錄根據條件多表插入 zt
- mysql 存在該記錄則更新,不存在則插入記錄的sqlMySql
- SQL 獲取插入後的最新一條記錄ID號SQL
- MySql避免重複插入記錄的幾種方法MySql
- MySQL 預插入的資料條數過多導致異常MySql
- mysql 批次更新與批次更新多條記錄的不同值實現方法MySql
- 記錄一次產品需求中使用的一條 MySQLMySql
- Oracle insert all一次插入多個表中Oracle
- mysql快速新增百萬條記錄的語句MySql
- mysql 插入時帶判斷條件MySql
- MYSQL使用記錄之:handlerMySql
- postgresql 多條記錄合併一條,或取最新的一條資料SQL
- mysql根據查詢結果批量更新多條資料(插入或更新)MySql
- MySQL問題記錄--python插入中文至MySQL提示SQLErroor:1366錯誤MySqlPython
- db2不記錄日誌插入記錄DB2
- 使用Direct-Path INSERT插入資料到表中
- MySQL 關聯表取最新一條記錄方案MySql
- 使用JdbcTemp查詢少了一條記錄JDBC
- MYSQL使用記錄之:infile/outfileMySql
- SQL 如何返回最大值所在的多條記錄SQL
- insert批量插入優化方案優化
- [CareerCup] 5.1 Insert Bits 插入位
- MySQL刪除重複記錄並保留第一條MySql
- 【Mysql】兩條insert 語句產生的死鎖MySql
- insert /*+ append */直接路徑插入APP