Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用另一个表中的数据计算列_Php_Mysql_Sql - Fatal编程技术网

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,看看下面我的答案。