【问题标题】:CSV to Mysql import script to match fieldsCSV 到 Mysql 导入脚本以匹配字段
【发布时间】:2011-12-05 09:20:15
【问题描述】:

是否有这样的脚本或将数据从 CSV 导入 mysql 的方法,它可以匹配第一个字段,而不是覆盖数据,只会更新行中缺少的字段或值?希望我已经足够清楚地解释了自己!

例子:

1,john,doe,programmer
2,jane,doe,accountant
3,judy,doe,manager
1,john,doe,cto

假设第一个字段是一个 ID,我希望脚本在记录不存在时插入,但在 ID 已经存在时更新,因此 John Doe 将首先作为程序员插入,然后更新为 CTO。

【问题讨论】:

    标签: php mysql csv


    【解决方案1】:

    试试下面的可能对你有帮助CSV TO MySQl

    祝你好运

    【讨论】:

      【解决方案2】:

      来自MySQL reference manual,在LOAD DATA section

      REPLACE 和 IGNORE 关键字控制输入行的处理 在唯一键值上复制现有行:

      If you specify REPLACE, input rows replace existing rows.
      In other words, rows that have the same value for a primary key or unique index
      as an existing row. See Section 12.2.7, “REPLACE Syntax”.
      
      If you specify IGNORE, input rows that duplicate an existing row
      on a unique key value are skipped. If you do not specify either
      option, the behavior depends on whether the LOCAL keyword is
      specified. Without LOCAL, an error occurs when a duplicate key value
      is found, and the rest of the text file is ignored. With LOCAL, the
      default behavior is the same as if IGNORE is specified; this is
      because the server has no way to stop transmission of the file in the
      middle of the operation.
      

      所以我相信,如果您将存储名称的字段设置为主键或唯一索引,您应该能够做到:

      LOAD DATA LOCAL INFILE 'file.csv'
      REPLACE INTO TABLE yourtable
      FIELDS TERMINATED BY ','
      LINES TERMINATED BY '\n'
      (id, name, surname, etc);
      

      更新:

      刚刚找到更多关于这个主题的资源:

      mysql duplicates with LOAD DATA INFILE

      http://support.modwest.com/content/6/253/en/how-do-i-import-delimited-data-into-mysql.html

      http://codeinthehole.com/archives/35-How-to-sync-a-MySQL-table-between-two-remote-databases..html

      【讨论】:

        猜你喜欢
        • 2020-04-04
        • 2011-04-25
        • 1970-01-01
        • 2016-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-05
        • 2012-06-01
        相关资源
        最近更新 更多