将Excel数据导入MySQL的关系表

kam*_*aci 4 mysql excel relational-database

我的MySQL数据库有三个表.第一个是城市,第二个是城镇,第三个是地区.每个城镇都有很多地区.我的桌子详情:

城市:

cityid,城市名称

城市:

townid,cityid,townname,大陆

区:

districtid,townid,districtname

我有一个excel文件,其中包含一个城市的城镇和地区名称.它有三列.

城市名称,城镇名称,地区名称

此excel表中的城市名称始终相同.由于其地区,城镇名称有重复.我的意思是对于一个城镇的每个地区:城市名称和城镇名称相同,地区名称与往常不同.例如:

城市名称,城镇名称,地区名称

XYA

XYB

XKC

XKD

小智 8

您可以将Excel工作表另存为CSV文件,然后使用LOAD DATA INFILE命令将此类文件导入临时MySQL表中,并使用相同的Excel工作表列,最后将临时表记录拆分为三个表"城市" ","城镇"和"地区".
一个前提:由于Excel文件中没有"id"字段,我想所有的id都是"auto_increment"字段; 此外,"城镇"表的"大陆"字段将始终设置为空白值,因为Excel表格中不存在此字段; 最后,我假设所有"名称"字段的最大长度为255个字符.
从您的示例数据开始,通过以CSV格式导出Excel工作表并保存(例如)到" C:\ Temp\excel.csv "文件中,您应该得到如下内容:

"city name","town name","district name"
"X","Y","A"
"X","Y","B"
"X","K","C"
"X","K","D"
Run Code Online (Sandbox Code Playgroud)

要将此文件导入MySQL数据库,请创建包含以下内容的" excel2mysql.sql "文件,然后执行该文件:

DROP TABLE IF EXISTS excel_table;
CREATE temporary TABLE excel_table (
  city_name VARCHAR(255),
  town_name VARCHAR(255),
  district_name VARCHAR(255)
) DEFAULT CHARSET utf8;

LOAD DATA LOCAL INFILE 'C:/Temp/excel.csv' 
INTO TABLE excel_table 
CHARACTER SET utf8
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 
IGNORE 1 LINES;

DROP TABLE IF EXISTS cities;
CREATE TABLE cities (
  city_id int NOT NULL auto_increment,
  city_name VARCHAR(255),
  primary key (city_id)
) DEFAULT CHARSET utf8;

INSERT INTO cities 
  SELECT distinctrow NULL, city_name 
    FROM excel_table 
   ORDER BY city_name;

DROP TABLE IF EXISTS towns;
CREATE TABLE towns (
  town_id int NOT NULL auto_increment,
  city_id int NOT NULL,
  town_name VARCHAR(255),
  continent VARCHAR(255),
  primary key (town_id)
) DEFAULT CHARSET utf8;

INSERT INTO towns 
  SELECT distinctrow NULL, city_id, town_name, '' 
    FROM excel_table, cities 
   WHERE cities.city_name=excel_table.city_name 
   ORDER BY town_name;

DROP TABLE IF EXISTS districts;
CREATE TABLE districts (
  district_id int NOT NULL auto_increment,
  town_id int NOT NULL,
  district_name VARCHAR(255),
  primary key (district_id)
)  DEFAULT CHARSET utf8;

INSERT INTO districts 
  SELECT distinctrow NULL, town_id, district_name 
    FROM excel_table, towns 
   WHERE towns.town_name=excel_table.town_name 
   ORDER BY district_name;
Run Code Online (Sandbox Code Playgroud)


Fio*_*ala 3

将 MySQL 与 Excel 和 ADO 结合使用的粗略示例。合适的连接字符串可以从http://connectionstrings.com获取

Dim cn As New ADODB.Connection
Dim sFile As String, scn As String, sSQL As String
Dim MySQLConnectionString As String

''It is probably better to use the name of the workbook
''eg C:\Docs\Cities.xls
sFile = ActiveWorkbook.FullName

''Access 2003 or less
scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes"";"
cn.Open scn

MySQLConnectionString = "ODBC;Driver={MySQL ODBC 5.1 Driver};Server=localhost;" _
& "Database=mydatabase;User=myuser;Password=mypass;Option=3;"

''Fields (columns) are case sensitive
''Insert cities from sheet1, you can also use a named range, 
''include or a range
sSQL = "INSERT INTO [" & MySQLConnectionString & "].Cities (Id,City) " _
& "SELECT Id,City FROM [Sheet$] WHERE Id Not In " _
& "(SELECT Id FROM [" & MySQLConnectionString & "].Cities )"

cn.Execute sSQL, dbFailOnError
Run Code Online (Sandbox Code Playgroud)