gpt4 book ai didi

mysql循环创建单个输出

转载 作者:行者123 更新时间:2023-11-29 22:35:56 25 4
gpt4 key购买 nike

问题

我有一个表'purchase',其中包含索引salesPersonNumberproductCodepurchaseIDproductCode 充当另一个表'product' 的外键,其中有一个索引price

还有另一个表'purchase info',其中包含键offerPrice。该表由一个复合键组成,其中键为 productCodepurchaseID

我希望能够添加 price,其中 salesPersonNumber = x,但如果 offerPrice> 0 ,用于将其添加到总计中(而不是价格)。

有什么办法可以用mysql来做到这一点吗?

尝试

设置与其他表的连接:

SELECT * 
FROM `purchase`
inner join `product` on purchase.productCode = product.productCode
inner join `purchase info` PI on purchase.productCode = PI.productCode AND purchase.purchaseID = PI.purchaseID

最终的 WHERE 语句

WHERE salesPersonNumber = "'.$x.'"
<小时/>

现在我需要想出一个循环?

伪代码

for each(number of rows belonging to salesPersonNumberX as row){
if(row['offerPrice'] > 0){
row['price'] = row['offerPrice'];
}
total += row['price'];
}
return total;
<小时/>

条款和条件

这需要仅在 SQL 中完成!!†

除了变量$x

最佳答案

是的。

无需深入研究架构,简单的答案是使用 case 语句。

https://dev.mysql.com/doc/refman/5.0/en/case.html

关于mysql循环创建单个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29551344/

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