Import CSV to Update rows in table(导入 CSV 以更新表中的行)
问题描述
大约有 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;
这篇关于导入 CSV 以更新表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:导入 CSV 以更新表中的行


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01