【问题标题】:How to multiply (2) cols from (2) different tables and input the results in a new col created in (1) of the tables如何将 (2) 个不同表中的 (2) 列相乘,并将结果输入到表 (1) 中创建的新列中
【发布时间】:2014-11-10 19:29:31
【问题描述】:
  1. 我在数据库中有 (2) 个表:Products、ProductSold

产品

--------------------    
| id | name | cost |    
--------------------    
101  | Jeans|  32       
102  | shoes|  53       

产品销售

----------------------------
| strNum | amtSold | sales |
----------------------------
 100    |   83    |  NULL
 100    |  105    |  NULL
  1. 我需要将 (amountSold * cost) 列相乘。

  2. 我需要在 ProductSold 表中的“sales”列中输入结果。

*注意:我有一个连接到我的数据库的 php 脚本,获取其中的数据,并将其显示在一个 html 表中。


|产品编号 |产品描述 |商店 | QTR |售出金额 |成本 |销售 |

  101                Jeans           100    1          83        32
  102                Shoes           100    1         105        53

【问题讨论】:

  • 你怎么知道牛仔裤的销量是83?
  • 给了。唯一没有给出的数据是销售列。
  • 我的意思是,没有连接产品和产品销售表的列,那么您如何获得该信息?
  • 其实有---> $sql = "select p.productId, p.name, ps.storeNumber, ps.qtr, ps.amountSold, p.cost, ps.sales FROM Products p内部加入 ProductSold ps on ps.productId = p.productId where storeNumber = 100";

标签: php mysql


【解决方案1】:

好的。假设您的数据库设置正确,这并不难,在这种情况下并非如此。现在,您的ProductSold 表中没有productId 列,这意味着amtSold 没有任何意义,因为我们不知道卖的是什么。所以,你的表格应该是这样的:

id | strNum | prodId | amtSold
1  | 100    | 101    | 83
2  | 100    | 102    | 105

这样计算就可以在这里完成。对于您的选择语句,它将类似于:

SELECT p.prodId, p.name, ps.strNum, p.cost, ps.amtSold FROM ProductSold ps, (p.cost * ps.amtSold) AS profit 
LEFT JOIN Products p ON p.id = ps.id;

获取基本信息。这将返回:

prodId | name  | strNum | cost | amtSold | profit
=================================================
101    | Jeans | 100    | 32   | 83      | 2656
102    | Shoes | 100    | 53   | 105     | 5565

现在,您不需要将利润直接存储在数据库中,只要您的查询有该计算。希望这有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多