2012-03-16 80 views
20

我有一个CSV看起来像这样,导入CSV到phpmyadmin的

candidate_id,show_on_site,first_name,surname,gender,DOB,showdob,Location,height,eyes,hair_colour,hair_length,accents,unions,training,url,visible,availability 
,26,urban talent,Strawberry,Shortcake,Female,11 Jan 1942,FALSE,Manchester,5'2,Brown,Black,Mid-length,Native Lancashire,Equity,Urban Talent TV & Drama Workshops,Strawberry-Shortcake---5.4.06.jpg,Yes,Yes 
,29,urban talent,Rainbow,Brite,Female,12 Oct 1970,FALSE,Manchester,5'7,Brown,Dark Brown,Long,"Native Manchester, others include - Cheshire, RP, Patois, Standard USA",Equity Member,"BA Acting Studies, Arden School of Theatre<br>Urban Talent TV & Drama Workshops",Rainbow Brite 1_1.jpg,Yes,Yes 
,31,urban talent,Webbigail,Vanderquack,Female,4 Jun 1947,FALSE,Manchester,5'0,Hazel,Blonde,Mid-length,"Native Manchester, others include - Liverpool, Cockney, Birmingham, West Country, Standard Scottish, Standard Welch, S Irish",,Manchester School of Acting<br>3 Years at David Johnson Acting Workshops,Webbigail Vanderquack web 1.jpg,Yes,Yes 
,33,urban talent,Smurfette,Smurf,Female,1 Jul 1979,FALSE,Manchester,5'2,Dark Brown,Dark Brown,Long,"Native Manchester, others include - Liverpool, RP, Lancashire, Birmingham, Cockney, Devon, Geordie, West Country, Glasgow, Edinburgh, South African, Standard & Southern US, Persian, Asian, Indian ~ good ear for accents",,"Manchester School of Acting, with Mark Hudson<br>North Cheshire Theatre College, with David Johnson<Oldham Theatre Workshop",Smurfette Smurf web 4.jpg,Yes,Yes 

是否有可能只需要插入这个数据到我的数据库中现有的列,所有我似乎可以将其插入它作为然后有列名称A,B,C,D,E等的新表。

回答

4

使用LOAD DATA INFILE SQL语句可以导入CSV文件,但不能更新数据。然而,你可以使用一个技巧。

  • 再创建一个临时表为进口
  • 负载使用到该表从CSC

    LOAD DATA LOCAL INFILE '/file.csv' 
    INTO TABLE temp_table 
    FIELDS TERMINATED BY ',' 
    LINES TERMINATED BY '\n' 
    (field1, field2, field3); 
    
  • UPDATE真正的表连接的表

    UPDATE maintable 
    INNER JOIN temp_table A USING (field1) 
    SET maintable.field1 = temp_table.field1 
    
35

在phpMyAdmin中,点击表格,然后点击点击页面顶部的导入标签。

浏览并打开csv文件。离开字符集。取消部分导入,除非你有一个巨大的数据集(或慢速服务器)。选择文件后,格式应该已经选择了“CSV”,如果不是,则选择它(不使用LOAD DATA)。如果要在导入之前清除整个表格,请选中“用文件替换表格数据”。如果您认为CSV文件中有重复项,请选中“忽略重复行”。现在的重要组成部分,将接下来的四个字段为这些值:

Fields terminated by: , 
Fields enclosed by: “ 
Fields escaped by: \ 
Lines terminated by: auto 

目前这些匹配除了“由终止字段”,默认为分号的默认值。

现在点击开始按钮,它应该运行成功。

+0

我不喜欢用于删除重复行的措辞。 “忽略重复行”可以解释为对重复行没有做任何事情(即忽略它们),这会导致它们被添加到表中。感谢您解释他们实际做了什么。 – Dennis 2013-03-10 16:39:17

+0

完美地工作......谢谢.. – 2014-06-10 18:24:12

+0

不得不改变'由......终止的字段:':由......终止的字段; – 2018-01-02 12:00:51

1

这是由于id(自动递增失踪)而发生的。如果您在文本编辑器中通过为ID字段添加逗号来编辑它,这将得到解决。

3

在phpMyAdmin v.4.6.5.2有一个复选框选项“该文件的第一行包含表的列名....”:

enter image description here

0

我不是phpmyadmin的粉丝。我用SQLyog代替。您只需在Object Browser(Ctrl + SHift + 1)中选择表格。只需选择表格>导入>使用加载本地导入CSV数据(Ctrl + Shift + M)。

enter image description here