我最初试图将CSV文件导入MySQL。然后有一个列zip可能具有多个可能的值。所以我决定使用JSON作为数据类型。
我在MySQL(5.7)中用于导入文件的命令是:
LOAD DATA INFILE 'data.csv' INTO TABLE citys FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS;
data.csv
city, zips, id
Heidrick,40949 40950,1840030467
Park City,42160,1840014395
Hindman,41822 41823 41824,1840014373
Mysql
CREATE TABLE mytable(city varchar(100), zips json, id varchar(20) NOT NULL, PRIMARY KEY (id)) ENGINE=INNODB;
然后
mysql> select city,zips,id from USGeoData_array where id=1840037882;
+---------------+----------------+-------------+
| city | zips | id |
+---------------+----------------+-------------+
|Prairie Ridge | [98360, 98391] | 1840037882
+---------------+----------------+-------------+
mysql> select json_type(zips) from USGeoData_array where id=1840037882;
+-----------------+
| json_type(zips) |
+-----------------+
| ARRAY |
+-----------------+
但是,某些zip前面有多个零,然后变成[123,234] ...
有人对如何转换它有想法吗?从官方文档中找不到太多有用的信息。 非常感谢!