mysql load data infile 重写,mysql LOAD DATA INFILE更新

I am currently using mySQL LOAD DATA INFILE to insert a csv file into my database. This csv file is downloaded to the server on a daily basis in order to keep product data up-to-date.

What I want to know is how can I update the table with the new csv and preserve the existing data where it's not different?

Here is my current statement:

LOAD DATA LOCAL INFILE '$file' REPLACE INTO TABLE products FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\\\' IGNORE 1 LINES (aw_product_id,merchant_id,merchant_image_url,aw_deep_link,description,in_stock,merchant_name,brand_name,display_price,product_name,rrp_price,merchant_category

This works fine but it replaces the ID column with a completely new set and also reverts columns that I want to ignore, back to the default state. For example I have a column called 'Published' with a value of 0 or 1. If I use REPLACE it sets that column back to 0.

How can I use REPLACE but ignore some columns?

解决方案

The answer to How can I use REPLACE but ignore some columns? is you can't: REPLACE allways replaces a complete row, not the single field values of that row.

The answer to Can I still achieve my goal though is Yes: My recommendation would be to LOAD DATA LOCAL INFILE into another table, then use a stored procedure or query to INSERT and UPDATE (as opposed to REPLACE) your main table. If you give us a bit more information (table structure, which column matches the loaded data with the existing data) we might be able to help you further.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值