简体   繁体   中英

How to insert multiple rows in mysql

I have a table name:: users with id and name in mysql, the id is primary key with auto increment and name is varchar(50) and now i have a bunch of unique names almost 70, how should i insert it in bulk, i know basic statements but i didn't found something best related to this problem

mysql> desc users;

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

The MySQL docs says:

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of comma-separated column values, with lists enclosed within parentheses and separated by commas. Example:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

Also you can use the Insert... Select... syntax:

INSERT INTO tbl_name (a,b,c)
SELECT a,b,c
FROM ...

Sorry man for me it was simple only insert into users (name) values ("name1"),("name2");

If you have all the information you want to insert inside file you might use LOAD DATA INFILE

LOAD DATA INFILE 'InfoToBeInsertedInsideDB.csv' 
INTO TABLE tbl_name
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

The csv looks like:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM