Update MySQL table from a local file

后端 未结 3 569
天命终不由人
天命终不由人 2020-12-31 14:28

I have a table in a database, and I\'d like to update a column which I have offline on a local file. The file itself has two columns

  1. an ID which corresponds to
相关标签:
3条回答
  • 2020-12-31 15:07

    It depends of the no of rows , If it is in hundreds make a script of update column and run it , but if it is in large volume import that file in to a new table and update your table with a join , and then drop the table

    0 讨论(0)
  • 2020-12-31 15:11

    I suggest you load your data into a temporary table, then use an INSERT ... SELECT ... ON DUPLICATE KEY UPDATE; for example:

    CREATE TEMPORARY TABLE temptable (
      id  INT UNSIGNED NOT NULL,
      val INT,
      PRIMARY KEY (id)
    ) ENGINE = MEMORY;
    
    LOAD DATA LOCAL INFILE '/path/to/file.txt' INTO temptable FIELDS TERMINATED BY ',';
    
    INSERT INTO my_table
    SELECT id, val FROM temptable
    ON DUPLICATE KEY UPDATE val = VALUES(val);
    
    DROP TEMPORARY TABLE temptable;
    
    0 讨论(0)
  • 2020-12-31 15:21

    Another way could be ...

    Since you already know the table name as well have the ID and actual value ... what you can do is ... directly write the update statements in a file, like

    update mytable set value_col = value where ID_col = ID;
    
    Second Update Statement
    
    Third Update statement
    
    .......
    

    Save the file as *.sql like, updatescript.sql and then execute that script directly like

    mysql -h <hostname> -u root -p <your_db_name> < "E:/scripts/sql/updatescript.sql"
    
    0 讨论(0)
提交回复
热议问题