gpt4 book ai didi

MySQL求和其他表的元素

转载 作者:行者123 更新时间:2023-11-29 18:14:46 24 4
gpt4 key购买 nike

我有以下表格:

产品:

id | price

发票:

id | product_id | amount | total

总计应包含产品价格与其金额的乘积。例如:

产品:

1 | 3.00

发票:

1 | 1 | 5 | 15.00

我试图通过键入以下内容来填充总列:

SELECT product_id, amount, (amount * SELECT price from products WHERE id=product_id) as 'total' FROM invoice;

这不起作用。我的问题是,如何将产品的值(value)添加到列总计中?

最佳答案

使用内部查询(您只是放错了括号)。

从发票中选择产品 ID、金额、金额 *(从 id=product_id 的产品中选择价格)作为“总计”;

或者使用 JOIN

从invoice.product_id=products.id上的发票内连接产品中选择product_id、金额、金额*价格作为“总计”

要更新发票表中的总计列:

更新发票集总计=金额*(从 id=product_id 的产品中选择价格);

关于MySQL求和其他表的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47121002/

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