我有一个XML文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<resultset statement="YOUR SQL STATEMENTS TO GENERATE THIS XML FILE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="personal_number">539</field>
<field name="firstname">Name</field>
<field name="lastname">Surname</field>
<field name="email">email.domain.com</field>
<field name="start_time">2011-04-02 13:30:00</field>
<field name="end_time">2011-04-02 18:15:00</field>
<field name="employee_category">1,2,4,5,22,37,38,39,41,43,44</field>
</row>
<row>
<field name="personal_number">539</field>
<field name="firstname">Name</field>
<field name="lastname">Surname</field>
<field name="email">email.domain.com</field>
<field name="start_time">2011-04-02 13:30:00</field>
<field name="end_time">2011-04-02 18:15:00</field>
<field name="employee_category">1,2,4,5,22,37,38,39,41,43,44</field>
</row>
<row>
<field name="personal_number">539</field>
<field name="firstname">Name</field>
<field name="lastname">Surname</field>
<field name="email">email.domain.com</field>
<field name="start_time">2011-04-02 13:30:00</field>
<field name="end_time">2011-04-02 18:15:00</field>
<field name="employee_category">1,2,4,5,22,37,38,39,41,43,44</field>
</row>
我正在尝试使用SQL语句在MySQL中导入它:
use databasename;
LOAD XML LOCAL INFILE '/pathtofile/file.xml' INTO TABLE my_tablename;
表my_tablename包含以下字段:
id (auto increment id)
personal_number(varchar)
firstname(varchar)
lastname(varchar)
email(varchar)
start_time(varchar)
end_time(varchar)
employee_category(varchar)
我收到错误: 错误代码:1136 列数与第1行的值计数不匹配
我正在使用MySQL 5.1.56
我认为发生此错误是因为数据库表具有字段ID,该字段在XML文件中不存在。如何使用内置函数的MySQL查询导入此XML文件,以便在导入期间跳过id列并依赖于id列的自动递增函数? 是否有一些更聪明的方法来处理MySQL文件导入? 也许有更好的语句允许指定列映射?
谢谢!
答案 0 :(得分:46)
您可以指定以下字段:
LOAD XML LOCAL INFILE '/pathtofile/file.xml'
INTO TABLE my_tablename(personal_number, firstname, ...);
答案 1 :(得分:5)
由于ID是自动递增,您还可以将ID = NULL指定为
LOAD XML LOCAL INFILE&#39; /pathtofile/file.xml' INTO TABLE my_tablename SET ID = NULL;