简体   繁体   English

当csv表标题与mySQL表标题不匹配时使用LOAD DATA INFILE语句?

[英]Using LOAD DATA INFILE statement when csv table headings don't match mySQL table headings?

What I'm looking to do is import some .CSV files into mySQL using php . 我想要做的是使用php将一些.CSV文件导入到mySQL中。

I'm going to be using the LOAD DATA INFILE statement but I have an issue: 我将使用LOAD DATA INFILE语句,但是有一个问题:

The headers in the .CSV file don't match up with the columns in the mySQL table, there are way more in the .CSV file. .CSV文件中的标题与mySQL表中的列不匹配,.CSV文件中还有更多内容。 I know I can ignore certain columnhs by using a variable like: 我知道我可以通过使用像这样的变量来忽略某些columnh:

$insert_query = "LOAD DATA LOCAL INFILE $csvfile
            INTO table $tableName
            FIELDS TERMINATED BY ','
            LINES TERMINATED BY ''
            (col1,col2,col3,@ignore,col4)";

What I would like to know is if I can assign the columns of data in the .CSV file to different columns in the mySQL table, EG mapping the first column of data in the .CSV to the fifth column of the mySQL table, kind of like: 我想知道的是,是否可以将.CSV文件中的数据列分配给mySQL表中的不同列,例如将.CSV中的第一列数据映射到mySQL表的第五列中,例如喜欢:

$insert_query = "LOAD DATA LOCAL INFILE $csvfile
            INTO table $tableName
            FIELDS TERMINATED BY ','
            LINES TERMINATED BY ''
            (col5,col2,col3,@ignore,col4)";

Is this possible? 这可能吗? If so, some examples would be greatly appreciated! 如果是这样,将不胜感激一些示例! :) :)

Thanks! 谢谢!

You do this using the appropriate syntax for your problem and specify a column list: 您可以使用适合您问题的语法来执行此操作,并指定列列表:

LOAD DATA INFILE 'path/to/your/file' INTO TABLE yourTable (col1,col2,...);

The manual states further: 手册进一步指出:

You must also specify a column list if the order of the fields in the input file differs from the order of the columns in the table. 如果输入文件中字段的顺序与表中列的顺序不同,则还必须指定列列表。 Otherwise, MySQL cannot tell how to match input fields with table columns. 否则,MySQL无法告知如何将输入字段与表列匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM