PHP MYSQL:返回每个唯一条目的行

时间:2016-08-21 14:02:32

标签: php mysql

我设法建立了一个PHP脚本,用我的网站记录访问者的地理位置。 我正在尝试编辑代码,以便为每个国家/地区提供多少点击量,例如美国:14; GB:2;等等... 这是返回" 132"在每列上,因此每个国家/地区都没有唯一的匹配。

$squery = 'SELECT `gcountry`, `dtime` FROM `allowed`'; 
$ress = mysqli_query($conn, $squery);
echo '<style>.tables {margin-left: 6cm;}</style>';  
echo '<table border="1" class="tables">';
echo'<th><font color="white">Country</font></th><th><font color="white">Date / Time</font></th><font color="white">Hits</font></th>';

while($data = mysqli_fetch_array($ress))
{
echo '<tr>';
echo '<td><font color="white">&nbsp;'.$data['gcountry'].'&nbsp;</font></td><td><font color="white">'.$data['dtime'].'&nbsp;</font></td><td><font color="white">'.mysqli_affected_rows($conn).'&nbsp;</font></td>';
echo '</tr>';
}

编辑帖子,所以...... 这是代码和附加表条目:

$squery = 'SELECT gcountry,COUNT(*) FROM allowed GROUP BY gcountry';
$ress = mysqli_query($conn, $squery);

echo '<style>.tables {margin-left: 6cm;}</style>';
echo '<table border="1" class="tables">'; echo'<th><font color="white">Country</font></th><font color="white">Hits</font></th>'; 

while ($data = mysqli_fetch_array($ress)) {
    echo'<tr>';
    echo '<td><font color="white">&nbsp;'.$data['gcountry'].'&nbsp;</font></td><td><td><font color="white">'.mysqli_affected_rows($conn).'&nbsp;</font></td>';
    echo'</tr>';
}

表格内容:

IP               |  UserAgent                 |       GCOUNTRY             |    GCITY                   | DTIME
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          GB                | London                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RU                | Moscow                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          GB                | London                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          GB                | London                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Lombard                    | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | San Antonio                | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Constanta                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Bucharest                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Iasi                       | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          NL                | Amsterdam                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Fort Lauderdale            | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Constanta                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Constanta                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Bucharest                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RU                | Moscow                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Irving                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Anaheim                    | 21-08-2016

1 个答案:

答案 0 :(得分:2)

查看the COUNT function以及它如何与GROUP BY一起使用:

  

之前,您检索了拥有宠物的人的姓名。如果您想知道每个拥有者有多少宠物,您可以使用COUNT()

mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;

+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Benny  |        2 |
| Diane  |        2 |
| Gwen   |        3 |
| Harold |        2 |
+--------+----------+