gpt4 book ai didi

php - 选择一个不同的值计数及其自身的值 1 查询

转载 作者:行者123 更新时间:2023-11-29 05:19:23 25 4
gpt4 key购买 nike

我正在尝试从我的数据库中接收不同的成本,以及该不同成本的计数。我得到了“不同的成本”,但没有得到运行下一个流程所需的“成本”。有人可以帮我连同计数一起返回 DISTINCT COST 本身吗?

 $sql="SELECT COUNT(DISTINCT COST) AS COSTQ FROM STATS WHERE PURCHASEID = ".$purchaseid;
$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$costcoun = $row['COSTQ'];
$cost = $row['COST']; // I have already tried ['DISTINCT COST'] which i had no luck with either.
// This is where I run my next process
}
} else {
echo "0 results";
}

最佳答案

group by 子句正是为这种情况设计的——它将查询分解为不同的值,并对每个组分别应用聚合函数:

SELECT   cost, 
COUNT(*) AS costq -- No need for distinct, GROUP BY handles it
FROM stats
WHERE purchaseid = 'something'
GROUP BY cost

关于php - 选择一个不同的值计数及其自身的值 1 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27916924/

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