在 MySQL / PHP 中返回记录数并忽略重复项

发布于 2025-01-01 09:18:24 字数 605 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

套路撩心 2025-01-08 09:18:24
SELECT COUNT(*)                             AS DistinctClicks
     , COUNT(DISTINCT IpAddress)            AS DistinctIpAddresses
     , COUNT(DISTINCT Browser)              AS DistinctBrowsers
     , COUNT(DISTINCT IpAddress, Browser)   AS DistinctUsers      --- hopefully
FROM JB_Statistics
WHERE month='$month'
SELECT COUNT(*)                             AS DistinctClicks
     , COUNT(DISTINCT IpAddress)            AS DistinctIpAddresses
     , COUNT(DISTINCT Browser)              AS DistinctBrowsers
     , COUNT(DISTINCT IpAddress, Browser)   AS DistinctUsers      --- hopefully
FROM JB_Statistics
WHERE month='$month'
柳絮泡泡 2025-01-08 09:18:24

根据你的描述不准确。

上述答案在 SQL 方面是正确的,您可以使用 GROUP BYDISTINCT 子句,但 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.

弱骨蛰伏 2025-01-08 09:18:24

您可以使用:

$pageViews = mysql_query( "SELECT COUNT(DISTINCT uniquefield) FROM JB_Statistics WHERE month='$month'" );
$row = mysql_fetch_array( $pageViews );
echo "<p>Month: $month, Total Hits: ", $row[0], "</p>";

You can use:

$pageViews = mysql_query( "SELECT COUNT(DISTINCT uniquefield) FROM JB_Statistics WHERE month='$month'" );
$row = mysql_fetch_array( $pageViews );
echo "<p>Month: $month, Total Hits: ", $row[0], "</p>";
清引 2025-01-08 09:18:24

按以下方式修改您的查询:

SELECT COUNT(DISTINCT ipfield) FROM JB_Statistics WHERE month='$month'"

这应该会产生您想要的结果。

Modify your query in the following way:

SELECT COUNT(DISTINCT ipfield) FROM JB_Statistics WHERE month='$month'"

That should produce what you want.

赠佳期 2025-01-08 09:18:24

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文