sql >> Databáze >  >> RDS >> Mysql

Najděte MAX SUM v MySQL

Zkuste toto:

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

Zde je samotný dotaz SQL pro snazší pochopení:

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. Zpracování nulového data MySQL pomocí EF Core

  2. Oracle Převeďte TIMESTAMP s časovým pásmem na DATE

  3. Jak modelovat pro snadnou údržbu databáze

  4. Oracle datum mezi dotazem