要進(jìn)行批量插入,我們需要使用帶括號(hào)的所有列名稱,并用“,”分隔。
讓我們看一個(gè)例子。首先,我們將創(chuàng)建一個(gè)表。以下是創(chuàng)建表的CREATE命令。
mysql> CREATE table MultipleRecordWithValues - > ( - > id int, - > name varchar(100) - > ); Query OK, 0 rows affected (0.88 sec)
登錄后復(fù)制
以下是批量插入的語法。
INSERT into yourTableName values(column1,column2,....N),(column1,column2,....N),(column1,column2,....N),...........N;
登錄后復(fù)制
應(yīng)用上述語法插入批量記錄。
mysql> insert into MultipleRecordWithValues values(1,'John'),(2,'Carol'),(3,'Bob'),(4,'Smith'); Query OK, 4 rows affected (0.16 sec) Records: 4 Duplicates: 0 Warnings: 0
登錄后復(fù)制
由于有 4 行受到影響,這意味著我們已成功插入記錄。要檢查表中是否存在所有記錄,請(qǐng)使用 SELECT 命令。
mysql> select *from MultipleRecordWithValues;
登錄后復(fù)制
以下是輸出。
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Smith | +------+-------+ 4 rows in set (0.00 sec)
登錄后復(fù)制
以上就是如何在MySQL中進(jìn)行批量插入?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!