sql >> Database teknologi >  >> RDS >> Mysql

Find MAX af SUM i MySQL

Prøv dette:

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

Her er SQL-forespørgslen alene for lettere forståelse:

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. Sådan får du den aktuelle dato og tid med tidszoneforskydning i PostgreSQL

  2. MySQL standardværdifejl med ON DUPLICATE KEY UPDATE

  3. Hvordan opsætter man pandas DataFrame til PostgreSQL-tabellen?

  4. Hvorfor tager en indsættelsesforespørgsel nogle gange så lang tid at fuldføre?