$test= "SELECT c_id, SUM(n_sell) FROM sell GROUP BY c_id ";
$resut = mysql_query($test) or die(mysql_error());
while($t = mysql_fetch_array($resut)){
echo "Number of sold: ". $t['SUM(n_sell)'] ." to". $t['c_id'] .":id of customer";
echo "<br />";
}
它向我的所有客户显示销售商品的数量,我需要向具有最大销售商品数量的特定客户展示。 例如,这是我的结果:
销售数量:11到2:客户ID 销售数量:103至3:客户ID
但我想要的只是展示:
销售数量:103至3:客户ID
我希望你们能得到我, 谢谢。
答案 0 :(得分:4)
试试这个:
$test = "SELECT c_id, MAX(Bought) AS MaxBought FROM (SELECT c_id, SUM(n_sell) AS Bought FROM sell GROUP BY c_id) AS tmp HAVING MAX(Bought) = tmp.Bought";
$resut = mysql_query($test) or die(mysql_error());
while($t = mysql_fetch_array($resut)){
echo "Number of sold: ". $t['MaxBought'] ." to". $t['c_id'] .":id of customer";
echo "<br />";
}
以下是SQL查询,以便于理解:
SELECT c_id, MAX(Bought) AS MaxBought
FROM (SELECT c_id, SUM(n_sell) AS Bought
FROM sell
GROUP BY c_id) AS tmp
HAVING MAX(Bought) = tmp.Bought
答案 1 :(得分:1)
有两种方法(我能想到)。两者都需要在SQL查询中进行更改。
溶胶。 1
$test= "SELECT c_id, MAX( SUM(n_sell) ) FROM sell GROUP BY c_id ";
溶胶。 2
$test= "SELECT c_id, SUM(n_sell) FROM sell GROUP BY c_id ORDER BY SUM(n_sell) DESC LIMIT 1";
代码的其余部分可能保持不变。
答案 2 :(得分:1)
使用ORDER BY
。