Heidel
Heidel

Reputation: 3254

MySQL add data from one table to another

There are 2 connected tables

uc_products with fields

vid
sell_price

uc_product_options with fields

nid
oid
price

and vid == nid

I need to get all prices from uc_product_options and add them to uc_products in sell_price column.

I know how to select all values I need
SELECT nid, oid, price FROM uc_product_options WHERE oid = 3;

but how to combine this query with UPDATE query for the second table?

Upvotes: 0

Views: 42

Answers (2)

Jens
Jens

Reputation: 69440

Update ... join should help you:

UPDATE uc_products a 
    JOIN uc_product_options  b ON a.nid = b.vid 
SET a.sell_price = b.price
where b.oid=3

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

Update the joined tables

update uc_products
join uc_product_options on uc_products.vid = uc_product_options.nid
set uc_products.sell_price_ = uc_product_options.price
where uc_product_options.oid = 3

Upvotes: 1

Related Questions