在现有的mysql表中插入新表信息?

时间:2013-07-29 01:28:15

标签: mysql sql

你好,大家好想问一下mysqldump。 如何在现有的mysql表中插入csv值,里面没有值? 因为我的.csv文件包含两列。 ID和NAME,我有一个有3列的表。一个是自动递增的id,另一个是brgy_name,最后是brgy_code。在我的csv中,ID列值应放在brgy_code中,NAME列值应放在brgy_name中。

Example:

"id","name"
"1001","Bangued"
"1002","Boliney"
"1003","Bucay"
"1004","Bucloc"
"1005","Daguioman"
"1006","Danglas"
"1007","Dolores"
"1008","La Paz"
"1009","Lacub"
"1010","Lagangilang"
"1011","Lagayan"
"1012","Langiden"
"1013","Licuan-Baay"
"1014","Luba"
"1015","Malibcong"
"1016","Manabo"
"1017","Penarrubia"
"1018","Pidigan"
"1019","Pilar"
"1020","Sallapadan"
"1021","San Isidro"
"1022","San Juan"
"1023","San Quintin"
"1024","Tayum"
"1025","Tineg"
"1026","Tubo"
"1027","Villaviciosa"
"1028","Butuan City"
"1029","Buenavista"
"1030","Cabadbaran"
"1031","Carmen"
.
.
.

在我的表中,里面没有行。这是我的表结构:

CREATE TABLE `ref_barangay` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `brgy_name` VARCHAR(50) NOT NULL,
    `brgy_code` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;

我的问题是如何插入我的csv文件?在我的csv文件中,列id是brgy_code。

Here's the what i want to do:
id    brgy_name        brgy_code
1     Bangued          1001
2     Boliney          1002
.
.
.

这是所有人我怎么能这样做?我想使用mysql转储,但我该怎么办?

1 个答案:

答案 0 :(得分:1)

尝试LOAD DATA INFILE

LOAD DATA INFILE '/path/to/your/file.csv'
INTO TABLE ref_barangay
    FIELDS TERMINATED BY ',' 
           OPTIONALLY ENCLOSED BY '"'
    LINES  TERMINATED BY '\n' -- or \r\n
    IGNORE 1 LINES
(@code, @name)
 SET brgy_name = @name, brgy_code = @code

输出

mysql> select * from ref_barangay;
+----+--------------+-----------+
| id | brgy_name    | brgy_code |
+----+--------------+-----------+
|  1 | Bangued      | 1001      |
|  2 | Boliney      | 1002      |
|  3 | Bucay        | 1003      |
|  4 | Bucloc       | 1004      |
...