Php 使用另一个表中的数据计算列
我有两张桌子:产品和设置 产品:Php 使用另一个表中的数据计算列,php,mysql,sql,Php,Mysql,Sql,我有两张桌子:产品和设置 产品: Name BuyPrice SellPrice ======================== Coke 15.00 0 背景 sellPricePercent ================= 50 我想通过使用表设置中的SellPrice百分比设置来设置表Products中的SellPrice值。因此,结果将是: Name BuyPrice SellPrice ======================== Coke 15.00
Name BuyPrice SellPrice
========================
Coke 15.00 0
背景
sellPricePercent
=================
50
我想通过使用表设置中的SellPrice百分比设置来设置表Products
中的SellPrice
值。因此,结果将是:
Name BuyPrice SellPrice
========================
Coke 15.00 30
如何执行此操作?您可以对表使用带笛卡尔积的select
select a.Name, a.BuyPrice, a.buyprice*(100/b.sellPricePercent)
from Products as a, Settings as b
或者使用选择列
select Name, BuyPrice, buyprice*(100/select(sellPricePercent
from Settings ))
from Products
试试这个:
update Products, Settings
set Products.SellPrice = Products.BuyPrice*(100/Settings.sellPricePercent);
如果不同的产品有不同的售价百分比,您可以加入product name上的表。不确定如何从15和50中获得30,但您需要加入或再选择,以便在查询中执行相同的操作是否要更新Products表?@AcatnamedNight是的,实际上我正要对下面的答案发表评论。你能帮我吗?@Odie,看看下面我的答案。