A single MySQL query for select top 3 within groupon-by? -


i have table deals, , columns name, amount, division, category. each division category combination, want find top 3 entries max amount. final result should in format: name, division, category.

   $db = new pdo($hostname,$username,$password);    $query = 'select name, division, category       deals division = :division , category = :category       order amount desc limit 3'; 

then create arrays of divisions , categories , loop them:

$top3s = array(); foreach($divisionarray $division) {   foreach($categoryarray $category)   {     $statement = $db->prepare($query);     $statement->bindvalue(':division', $division);     $statement->bindvalue(':category', $category);     $statement->execute();     $top3 = $statement->fetch(pdo::fetch_assoc);     $statement->closecursor();      array_push($top3s, $top3);   } } print_r $top3s; 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -