我有這樣的表:跳過與LOAD DATA INFILE第一列
mysql> show create table final\G;
*************************** 1. row ***************************
Table: final
Create Table: CREATE TABLE `final` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`cdatetime` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`district` varchar(255) NOT NULL,
`beat` varchar(255) NOT NULL,
`grid` varchar(255) NOT NULL,
`crimedescr` varchar(255) NOT NULL,
`ucr_ncic_code` varchar(255) NOT NULL,
`latitude` varchar(255) NOT NULL,
`longitude` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
而且我有CSV文件看起來像這樣:
cdatetime,address,district,beat,grid,crimedescr,ucr_ncic_code,latitude,longitude
1/1/06 0:00,3108 OCCIDENTAL DR,3,3C ,1115,10851(A)VC TAKE VEH W/O OWNER,2404,38.55042047,-121.3914158
1/1/06 0:00,2082 EXPEDITION WAY,5,5A ,1512,459 PC BURGLARY RESIDENCE,2204,38.47350069,-121.4901858
1/1/06 0:00,4 PALEN CT,2,2A ,212,10851(A)VC TAKE VEH W/O OWNER,2404,38.65784584,-121.4621009
1/1/06 0:00,22 BECKFORD CT,6,6C ,1443,476 PC PASS FICTICIOUS CHECK,2501,38.50677377,-121.4269508
我想要做的就是載荷CSV文件放入表格最後。問題是,csv文件沒有ID列,所以我想是否有可能以某種方式告訴mysql跳過列ID並將數據加載到其餘列,但ID必須使用。所以理想情況下它應該是這樣的:
「1/1/06 0:00,3108 OCCIDENTAL DR,3,3C,1115,10851(A)VC TAKE VEH W/O OWNER,2404,38.55042047,-121.3914158 「被加載到列和MySQL自動添加1列ID,然後」1/1/06 0:00,2082 EXPEDITION WAY,5,5A,1512,459 PC BURGLARY RESIDENCE,2204,38.47350069,-121.4901858「被加載和MySQL的加入2至ID列等等等等。
最近用戶「陰影」告訴我,我應該指定我要加載的列,所以我做了這樣的事情:
load data infile '/SacramentocrimeJanuary2006.csv' INTO TABLE final (cdatetime, address, district, beat, grid, crimedescr, ucr_ncic_code, latitude, longitude);
Mysql的返回:
ERROR 1261 (01000): Row 1 doesn't contain data for all columns
根據mysql負載數據infile手動字段分隔符不是「,」所以我試圖通過在我的語句結尾添加FIELDS TERMINATED BY','來改變它,但這打破了查詢。這裏的正確語法是什麼?
感謝
ANSWER
mysql> CREATE TABLE `final` (
-> `id` int(4) NOT NULL AUTO_INCREMENT,
-> `cdatetime` longtext NULL,
-> `address` longtext NULL,
-> `district` longtext NULL,
-> `beat` longtext NULL,
-> `grid` longtext NULL,
-> `crimedescr` longtext NULL,
-> `ucr_ncic_code` longtext NULL,
-> `latitude` longtext NULL,
-> `longitude` longtext NULL,
-> PRIMARY KEY (`id`)
->) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.17 sec)
mysql> LOAD DATA infile '/SacramentocrimeJanuary2006.csv' INTO TABLE final FIELDS TERMINATED BY ',' lines terminated by '\r' IGNORE 1 ROWS (cdatetime, address, district, beat, grid, crimedescr, ucr_ncic_code, latitude, longitude);
Query OK, 7584 rows affected (0.08 sec)
Records: 7584 Deleted: 0 Skipped: 0 Warnings: 0
的MySQL> LOAD DATA INFILE '/SacramentocrimeJanuary2006.csv' - ENCLOSED BY '> INTO TABLE TERMINATED BY最後場 ''' IGNORE 1個ROWS - >(cdatetime ,地址,地區,節拍,網格,犯罪記錄,ucr_ncic_code,緯度,經度); 查詢OK,0行受影響(0.00秒) **記錄:0刪除:0跳過:0警告:0 ** – jonasv