2017-11-09 6 views
1

Ich möchte die zweite Spalte einer CSV-Datei in MySQL importieren. Hier ist die CSV-Datei:Importieren Sie nur die zweite CSV-Spalte in MySQL

Name,Source,Follows 
John,Youtube,Y 
Kat,FB,N 
Jacob,Twitter,N 

Hier ist der Code, den ich bisher habe:

DROP TABLE temp; 

CREATE TABLE temp 
(ID   INT    AUTO_INCREMENT   primary key, 
sn   VARCHAR(50) 
); 

DESCRIBE temp; 

LOAD DATA LOCAL INFILE '/Users/...temp.csv' INTO TABLE Person 
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' 
IGNORE 1 LINES 
(@col2) set [email protected]; 

SELECT * FROM temp; 

Allerdings bekomme ich, dass es eine leere Menge ist.

Antwort

0

Versuchen:

Datei: /path/to/temp.csv:

Name,Source,Follows 
John,Youtube,Y 
Kat,FB,N 
Jacob,Twitter,N 

MySQL-Kommandozeilen:

mysql> DROP TABLE IF EXISTS `temp`; 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `temp` (
    -> `ID` INT AUTO_INCREMENT PRIMARY KEY, 
    -> `sn` VARCHAR(50) 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> LOAD DATA LOCAL INFILE '/path/to/temp.csv' 
    -> INTO TABLE `temp` 
    -> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' 
    -> IGNORE 1 LINES 
    -> (@`null`, `sn`, @`null`); 
Query OK, 3 rows affected (0.00 sec) 
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 

mysql> SELECT `ID`, `sn` 
    -> FROM `temp`; 
+----+---------+ 
| ID | sn  | 
+----+---------+ 
| 1 | Youtube | 
| 2 | FB  | 
| 3 | Twitter | 
+----+---------+ 
3 rows in set (0.00 sec) 

UPDATE

mysql> LOAD DATA LOCAL INFILE '/tmp/temp.csv' 
    -> INTO TABLE `temp` 
    -> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' 
    -> IGNORE 1 LINES 
    -> (@`null`, `sn`); 
Query OK, 3 rows affected, 3 warnings (0.00 sec) 
Records: 3 Deleted: 0 Skipped: 0 Warnings: 3 

mysql> SHOW WARNINGS; 
+---------+------+---------------------------------------------------------------------------+ 
| Level | Code | Message                 | 
+---------+------+---------------------------------------------------------------------------+ 
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns | 
| Warning | 1262 | Row 2 was truncated; it contained more data than there were input columns | 
| Warning | 1262 | Row 3 was truncated; it contained more data than there were input columns | 
+---------+------+---------------------------------------------------------------------------+ 
3 rows in set (0.00 sec) 

mysql> SELECT `ID`, `sn` 
    -> FROM `temp`; 
+----+---------+ 
| ID | sn  | 
+----+---------+ 
| 1 | Youtube | 
| 2 | FB  | 
| 3 | Twitter | 
+----+---------+ 
3 rows in set (0.00 sec) 
+0

Dies tat Arbeit - Danke! Aber gibt es eine Möglichkeit, dies für eine viel größere CSV-Datei zu tun? Nehmen wir an, es hatte 100 Spalten. Gibt es eine Möglichkeit, nur die Informationen der zweiten Spalte zu erhalten? – billyl320

+0

@ billyl320: Siehe aktualisierte Antwort. 'Wenn eine Eingabezeile zu viele Felder enthält, werden die zusätzlichen Felder ignoriert und die Anzahl der Warnungen wird erhöht.', siehe [13.2.6 LOAD DATA INFILE Syntax] (https://dev.mysql.com/doc/refman/ 5.7/de/load-data.html). – wchiquito

+0

Ah okay - danke. Irgendeine Idee, wie sich das ändern könnte, wenn ich die 50. Spalte von 100 bekommen wollte? – billyl320

Verwandte Themen