gpt4 book ai didi

php - 将 GROUP BY 结果作为分隔变量返回

转载 作者:行者123 更新时间:2023-11-29 02:03:33 25 4
gpt4 key购买 nike

我是 mysql/php 的新手,我正在尝试使用

构建自定义查询
$query = "SELECT expo_location,  SUM(total_sale) FROM _sales_db WHERE day IN('Friday') GROUP BY expo_location"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "<span style='font-weight:normal;color:#cfcfcf;'>"."Total ". $row['expo_location']. " = $". number_format((float)$row['SUM(total_sale)'], 2, '.', '')."</span>";
echo "<br />";
}

哪个返回:

//
Total Canton = $295.94
Total Malone = $980.24
Total Massena = $1187.10
//

我希望使用 GROUP BY 将“expo_location”名称作为变量返回。

Example
$Canton =
$Malone =
$Massena =

总而言之,将所有条目放在一列中并将它们组合在一起并将它们作为变量返回?

最佳答案

下面的代码将为您创建一个关联数组,这样您就可以执行类似于 $expo_locations['Canton'] 的操作,其中将包含 295.94

    $query = "SELECT expo_location,  SUM(total_sale) FROM _sales_db WHERE day IN('Friday') GROUP BY expo_location"; 

$result = mysql_query($query) or die(mysql_error());

$expo_locations = array();

// Print out result
while($row = mysql_fetch_array($result)){
$expo_locations[$row['expo_location']] = number_format((float)$row['SUM(total_sale)'], 2, '.', '');
}

关于php - 将 GROUP BY 结果作为分隔变量返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10456717/

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