sql >> Database >  >> RDS >> Mysql

Vind de MAX van SUM in MySQL

Probeer dit:

$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 />";
}

Hier is alleen de SQL-query voor een beter begrip:

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. Grotere SQL-bestanden importeren in MySQL

  2. MySQL-databaseschema's vergelijken

  3. PHP kan geen verbinding maken met mysql via python

  4. Maak een scalaire door de gebruiker gedefinieerde functie in SQL Server