导入 CSV 以更新表中的行

2021-12-25 00:00:00 csv import mysql phpmyadmin

大约有 26K 个产品(帖子),每个产品都有这样的元值:

There are approximately 26K products (posts) and each product has meta values like this:

post_id 列是 db 中的产品 ID,_sku (meta_key) 是每个产品的唯一 ID.

The post_id column is the product id in db and the _sku (meta_key) is the unique id for each product.

我收到了一个新的 CSV 文件,该文件更新了每种产品的 _sale_price (meta_key) 的所有值 (meta_value).CSV 文件如下所示:
SKU、销售价格

I've received a new CSV file that updates all of the values (meta_value) for _sale_price (meta_key) of each product. The CSV file looks like:
SKU, Sale Price

如何导入此 CSV 以仅更新基于 post_id(产品 ID)的 _sale_price 行 &_sku 值?

How do I import this CSV to update only the _sale_price row based on the post_id (product id) & _sku value?

输出示例:

我知道如何在 PHP 中通过循环遍历 CSV 并选择 &为每个单个产品执行更新,但这似乎效率低下.

I know how to do this in PHP by looping through the CSV and selecting & executing an update for each single product but this seems inefficient.

最好使用 phpMyAdmin 并使用 LOAD DATA INFILE.

Preferably with phpMyAdmin and by using LOAD DATA INFILE.

推荐答案

您可以使用临时表来保存更新数据,然后运行单个更新语句.

You can use temporary table to hold the update data and then run single update statement.

CREATE TEMPORARY TABLE temp_update_table (meta_key, meta_value)

LOAD DATA INFILE 'your_csv_pathname' 
INTO TABLE temp_update_table FIELDS TERMINATED BY ';' (meta_key, meta_value); 

UPDATE "table"
INNER JOIN temp_update_table on temp_update_table.meta_key = "table".meta_key
SET "table".meta_value = temp_update_table.meta_value;

DROP TEMPORARY TABLE temp_update_table;

相关文章