我想计算将邮政编码输入数据库的次数。我不确定我是否使用了正确的功能。最后,我还需要将邮政编码按数据库中输入的年份分开。我知道如何分开岁月。我真正需要帮助的是计算重复的条目。 这是我的代码。
$sql = 'SELECT * FROM zip ORDER BY time_register';
$result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql");
$row = mysql_fetch_array($result);
do{
$visitor_zip= array();
$register = $row['time_register'];
$register_year = date(Y,$register);
print_r(array_count_values($visitor_zip));
} while($row = mysql_fetch_array($result))
回答:这是我的代码。
$sql = "SELECT `visitor_zip` AS `zip`, COUNT(`visitor_zip`) AS `cnt`
FROM `zip`
GROUP BY `visitor_zip`
ORDER BY visitor_zip";
$result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql");
print '<table class="zip"><tr><td><b>Zip</b></td><td># of</td></tr>';
while($row = mysql_fetch_array($result)) {
print '<tr><td>' . $row['zip'] .'</td><td>'. $row['cnt'] . '</td></tr>';
}
print '</table>';
答案 0 :(得分:0)
我认为这应该有效:
SELECT COUNT(*) AS cnt, zipcode FROM zip
GROUP BY zipcode
ORDER BY time_register
答案 1 :(得分:0)
select count(ZIPCODEFIELD) as cnt, ZIPCODEFIELD, time_register FROM zip
GROUP BY zipcode,time_register