如何将数据从 CSV 导入 MySQL?

How I can Import data from CSV to MySQL?(如何将数据从 CSV 导入 MySQL?)

本文介绍了如何将数据从 CSV 导入 MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构的 csv 文件:

I have csv file with following structure:

    A   BA0011  U206    NAME    0000000000000149.00     000000.00  0000000000000118.93  S   N   N
    A   BB0011  U206    NAME    0000000000000150.00     000000.00  0000000000000118.93  S   N   N
    A   BC0011  U206    NAME    0000000000000151.00     000000.00  0000000000000118.93  S   N   N
    A   BD0011  U206    NAME    0000000000000152.00     000000.00  0000000000000118.93  S   N   N
    A   BE0011  U206    NAME    0000000000000153.00     000000.00  0000000000000118.93  S   N   N

使用以下 MySQL 函数如何导入 MySQL 表中的第一列和第三列:

Using following MySQL Function How I can import first and third column in MySQL table:

LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
  FIELDS TERMINATED BY 	' ENCLOSED BY '"'
  LINES TERMINATED BY '
'
  IGNORE 1 LINES;

我为我的英语道歉!

提前致谢!

推荐答案

您可以指定列并将不需要的列标记为@dummy".

You can specify the columns and mark the unnecessary columns as '@dummy'.

LOAD DATA INFILE 'data.csv'
INTO TABLE t1
(column1, @dummy, column2, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
FIELDS TERMINATED BY '	' ENCLOSED BY 
LINES TERMINATED BY '
'
IGNORE 1 LINES;

根据需要替换 t1column1column2.要设置数据文件中没有的其他列,您可以这样做:

Replace t1, column1 and column2 as you like. To set other columns which are not in the data file, you can do it like this:

LOAD DATA INFILE 'data.csv'
INTO TABLE t1
(column1, @dummy, column2, @dummy, @val, @dummy, @dummy, @dummy, @dummy, @dummy)
FIELDS TERMINATED BY '	' ENCLOSED BY 
LINES TERMINATED BY '
'
IGNORE 1 LINES
SET column3 = "test", column4 = CURRENT_TIMESTAMP, column5 = @val/10;

为了进一步参考,我建议你看一下MySQL 参考.

For further reference, I recommend you to take a look at the MySQL reference.

这篇关于如何将数据从 CSV 导入 MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将数据从 CSV 导入 MySQL?