Odie Odie - 1 month ago 7
MySQL Question

Calculate Column Using Data from Another Table

I have two tables: Products and Settings.

Products:

Name BuyPrice SellPrice
========================
Coke 15.00 0


Settings

sellPricePercent
=================
50


I want to set the
SellPrice
from table
Products
value by using
sellPricePercent
from table
settings
. So the result would be:

Name BuyPrice SellPrice
========================
Coke 15.00 30


How do I do this?

Answer

Try this out:

update Products, Settings 
set Products.SellPrice = Products.BuyPrice*(100/Settings.sellPricePercent);

If you would ever have different sellPricePercent for different products, you could join the tables on product name.

Comments