gpt4 book ai didi

php - 如何将 (2) 个不同表中的 (2) 列相乘,并将结果输入到表 (1) 中创建的新列中

转载 作者:行者123 更新时间:2023-11-29 12:35:28 27 4
gpt4 key购买 nike

  1. 我的数据库中有 (2) 个表:Products、ProductSold

产品

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

产品已售出

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

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

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

    <小时/>

    |产品编号|产品描述 |商店 |季度报告 |销售量 |成本|销售 |

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

    最佳答案

    好的。假设您的数据库设置正确,这并不难,但在本例中并非如此。目前,您的 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

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

    关于php - 如何将 (2) 个不同表中的 (2) 列相乘,并将结果输入到表 (1) 中创建的新列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26851476/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com