0

我有一个表交易,列是名称、金额、部门、类别。对于每个部门类别组合,我想找到数量最多的前 3 个条目。最终结果应采用以下格式:名称、部门、类别。

4

1 回答 1

0
   $db = new PDO($hostname,$username,$password);
   $query = 'Select name, division, category 
     From deals Where division = :division And category = :category 
     ORDER BY amount DESC Limit 3';

然后创建所有部门和类别的数组并循环它们:

$top3s = array();
foreach($divisionArray as $division)
{
  foreach($categoryArray as $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;
于 2013-04-18T19:44:57.137 回答