在 MySQL / PHP 中返回记录数并忽略重复项
我有一个 mySQL 表,其中包含访问过我的网站的用户的记录。我想要做的是找出我的表中有多少条记录,但只计算一次重复值,以便找出有多少唯一访问者。
我使用以下代码来查找每个月的点击次数。
$pageViews = mysql_query("SELECT * FROM JB_Statistics WHERE month='$month' ");
$numRowsPV = mysql_num_rows($pageViews) ;
while ($row = mysql_fetch_assoc($pageViews)) {
$month = $row['month'];
}
echo "<p>Month: $month, Total Hits: $numRowsPV </p>";
表 JB_Statistics 还有一个 ip 地址和浏览器字段,其中包含每次点击的唯一信息,那么有没有一种方法可以使用它来修改上面的代码并找到每月独立访问者?
如果这很简单,我深表歉意,但我执行的每次搜索都会返回有关仅查找重复记录的主题。
I have a mySQL table with records of users who have visited my site. What I want to do is find out how many records there are in my table, but only counting duplicate values once in order to find out how many unique visitors there are.
I'm using the following code to find how many hits I'm getting each month.
$pageViews = mysql_query("SELECT * FROM JB_Statistics WHERE month='$month' ");
$numRowsPV = mysql_num_rows($pageViews) ;
while ($row = mysql_fetch_assoc($pageViews)) {
$month = $row['month'];
}
echo "<p>Month: $month, Total Hits: $numRowsPV </p>";
The table JB_Statistics also has an ip address and browser field with unique information for each hit, so is there a way of using this to modify the code above and find the unique visitors per month?
Apologies if this is straight forward but every search I carry out returns topics about only finding duplicate records.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据你的描述不准确。
上述答案在 SQL 方面是正确的,您可以使用 GROUP BY 或 DISTINCT 子句,但 IP 地址不会为您提供唯一访问者。单个 IP 后面可以有任意数量的用户。
您可能必须重新考虑使用什么来识别每条记录的独特之处。
Not accurately based on your description.
The above answers are correct in terms of SQL, you could use a GROUP BY or DISTINCT clause but IP address does not give you unique visitors. There can be any number of users behind a single IP.
You may have to rethink what you're using to identify what's unique about each record.
您可以使用:
You can use:
按以下方式修改您的查询:
这应该会产生您想要的结果。
Modify your query in the following way:
That should produce what you want.
使用 GROUP BY:
SELECT COUNT(*) FROM JB_Statistics WHERE Month='$month' GROUP BY ip_address
use GROUP BY:
SELECT COUNT(*) FROM JB_Statistics WHERE month='$month' GROUP BY ip_address