仅在有键匹配的地方更新 table
updating table only where there is a key match
起初我想通过导入 csv 文件来更新 table(产品价格),但我认为这是不可能的。
所以我创建了一个临时 table 并导入了 2 列:products_id、products_price
我想试过 运行 这个 sql:
UPDATE products p SET p.products_price=(
SELECT t.products_price
FROM temp_table t
WHERE t.products_id=p.products_id
)
这更新了所有 products_id 匹配的价格,但也将所有其他产品设置为 0.00
我该怎么做才能只在匹配时更新而忽略其他 product_ids?
您可以使用像
这样的更新连接查询
UPDATE products p
JOIN temp_table t ON t.products_id=p.products_id
SET p.products_price = t.products_price;
您可以使用内部联接
update table1 t1
inner join table2 t2
set column_name = t2.column_name
where t1.id =t2.id
起初我想通过导入 csv 文件来更新 table(产品价格),但我认为这是不可能的。
所以我创建了一个临时 table 并导入了 2 列:products_id、products_price
我想试过 运行 这个 sql:
UPDATE products p SET p.products_price=(
SELECT t.products_price
FROM temp_table t
WHERE t.products_id=p.products_id
)
这更新了所有 products_id 匹配的价格,但也将所有其他产品设置为 0.00
我该怎么做才能只在匹配时更新而忽略其他 product_ids?
您可以使用像
这样的更新连接查询UPDATE products p
JOIN temp_table t ON t.products_id=p.products_id
SET p.products_price = t.products_price;
您可以使用内部联接
update table1 t1
inner join table2 t2
set column_name = t2.column_name
where t1.id =t2.id