按国家和计数对结果进行分组
group result by country and count
我有以下代码;
$uid = '21';
$select = $dbh->prepare("SELECT * FROM `reports` WHERE `profileuid` = ?");
$select->bindParam(1,$uid, PDO::PARAM_STR);
$select->execute();
foreach($select as $row){
$country = $row['country'];
echo $country;
}
和以下数据库结构;
目前上面的代码给出了这个输出
United Kingdom
United Kingdom
United States
我想要的输出
United Kingdom 2
United States 1
感谢您的帮助;我自己会尝试这样做,但我不知道如何进行这样的计数。这可能是 GROUP
?
您可以将查询重写为:
SELECT Location, COUNT(1) as NoOfEntries
FROM `reports` WHERE `profileuid` = ?
GROUP BY Location
ORDER BY COUNT(1) DESC
它将国家分组并给出计数。
我有以下代码;
$uid = '21';
$select = $dbh->prepare("SELECT * FROM `reports` WHERE `profileuid` = ?");
$select->bindParam(1,$uid, PDO::PARAM_STR);
$select->execute();
foreach($select as $row){
$country = $row['country'];
echo $country;
}
和以下数据库结构;
目前上面的代码给出了这个输出
United Kingdom
United Kingdom
United States
我想要的输出
United Kingdom 2
United States 1
感谢您的帮助;我自己会尝试这样做,但我不知道如何进行这样的计数。这可能是 GROUP
?
您可以将查询重写为:
SELECT Location, COUNT(1) as NoOfEntries
FROM `reports` WHERE `profileuid` = ?
GROUP BY Location
ORDER BY COUNT(1) DESC
它将国家分组并给出计数。