我的MySQL数据库有三个表。第一个是城市,第二个是城镇,第三个是地区。每个城镇都有很多地区。我的表格详情:
城市:
cityid,城市名称
城镇:
townid,cityid,townname,continent
区:
districtid,townid,districtname
我有一个excel文件,其中包含一个城市的城镇和地区名称。它有三列。
城市名称,城镇名称,地区名称
此excel表中的城市名称始终相同。由于其地区,城镇名称有重复。我的意思是对于一个城镇的每个地区:城市名称和城镇名称相同,地区名称与往常不同。例如:
城市名称,城镇名称,地区名称
X Y A
X Y B
X K C
X K D
答案 0 :(得分:8)
您可以save your Excel sheet as in a CSV file,然后使用LOAD DATA INFILE命令在Excel工作表的相同列中导入此类文件,最后在三个表中拆分临时表记录“城市“,”城镇“和”区“ 一个前提:由于Excel文件中没有“id”字段,我想所有的id都是“auto_increment”字段;此外,“城镇”表的“大陆”字段将始终设置为空白值,因为Excel表格中不存在此字段;最后,我假设所有“名称”字段的最大长度为255个字符 从您的示例数据开始,通过以CSV格式导出Excel工作表并保存(例如)到“ C:\ Temp \ excel.csv ”文件中,您应该得到如下内容:< BR />
"city name","town name","district name"
"X","Y","A"
"X","Y","B"
"X","K","C"
"X","K","D"
要将此文件导入MySQL数据库,请创建一个包含以下内容的“ excel2mysql.sql ”文件,并execute it:
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;
答案 1 :(得分: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
答案 2 :(得分:0)
如果您的区,城镇或城市名称不是纯ASCII(带重音字符),那么您可能需要对@Enzino解决方案进行一次或调整;可能只是在任何用户说明中添加一个脚注,以指示用户在将工作表保存为CSV文件时,他们需要在另存为对话框框中选择:工具 - >&gt;网页选项选项,在结果对话框中选择编码标签,并设置字符集以匹配相应的mySQL表格: Unicode(UTF- 8)保存之前。
或者,根据执行导入的内容,我的下列answer可以帮助他们将数据直接输入 excel_table 。