MySQL选择记录,计算重复项
我有一个帖子表,每个帖子都有一个IP地址,我想通过计算它在数据库中出现的次数来查看该IP发布的次数,然后将其出现在屏幕上的次数。基本上是这样的:
MySQL 表:
id entry ip
1 abc 19.123.14.5
2 cde 19.123.14.5
3 efg 12.231.22.9
我希望代码能够接受它,计算重复项,然后像这样输出计数:
id entry ip count
1 abc 19.123.14.5 2
2 efg 12.231.22.9 1
到目前为止,这是我的代码(不起作用):
$result = mysql_query("SELECT ip, entry, id, COUNT(ip) AS A FROM table_name AS C GROUP BY ip HAVING COUNT A > 1 ORDER BY id DESC");
$i = 0;
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$entry = $row['entry'];
$ip = $row['ip'];
$count = ?????;
$i++;
?>
<tr width="100%" align="center">
<td><?php echo $i; ?></td>
<td><?php echo $id; ?></td>
<td><?php echo $entry; ?></td>
<td><?php echo $ip; ?></td>
<td><?php echo $count ?></td>
<td>
<form style="display:inline;" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" value="<?php echo $ip; ?>" name="ip" />
<input type="hidden" value="<?php echo $id; ?>" name="id" />
<input type="submit" value="Ban IP" name="submit" />
</form>
</td>
</tr>
<?php
}
任何帮助将不胜感激!
编辑: 不起作用:首先,从我的代码中可以明显看出,变量 $count 没有分配任何内容,因为我不知道要放在那里。其次,我得到这个错误:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
I have a table of posts, each post has an IP address, I want to see how many times that IP has posted by counting how many times it occurs in the database, and then putting the number of times it's appeared on the screen. Basically this:
MySQL Table:
id entry ip
1 abc 19.123.14.5
2 cde 19.123.14.5
3 efg 12.231.22.9
I want the code to take that, count duplicates, and ouput the count like so:
id entry ip count
1 abc 19.123.14.5 2
2 efg 12.231.22.9 1
Here's my code (that doesn't work) so far:
$result = mysql_query("SELECT ip, entry, id, COUNT(ip) AS A FROM table_name AS C GROUP BY ip HAVING COUNT A > 1 ORDER BY id DESC");
$i = 0;
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$entry = $row['entry'];
$ip = $row['ip'];
$count = ?????;
$i++;
?>
<tr width="100%" align="center">
<td><?php echo $i; ?></td>
<td><?php echo $id; ?></td>
<td><?php echo $entry; ?></td>
<td><?php echo $ip; ?></td>
<td><?php echo $count ?></td>
<td>
<form style="display:inline;" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" value="<?php echo $ip; ?>" name="ip" />
<input type="hidden" value="<?php echo $id; ?>" name="id" />
<input type="submit" value="Ban IP" name="submit" />
</form>
</td>
</tr>
<?php
}
Any help would be much appreciated!
EDIT:
Doesn't work: Well firstly, as obvious from my code, the variable $count has nothing assigned to it as I have no idea what to put there. Secondly, I get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您想按 IP 和条目进行分组,因此您缺少执行 MAX(id) 操作。您可以删除
Having>1
因为您希望根据示例输出看到 counts =1示例脚本:
生成:
You were missing to do MAX(id) since you want to group by IP and entry. You can drop the
Having>1
since you want to see counts =1 according to your sample outputSample script:
Produces:
您的
ip=19.123.14.5
有不同的id
和entries
。您想选择其中哪一位?仅计算
ip
并忽略id
和entry
:用于从可能连接到同一
的许多行中选择随机行>ip
(这是 MySQL 特定的,在其他数据库下不起作用):用于选择所有行以及
ip
计数:You have different
id
s andentries
forip=19.123.14.5
. Which one of them you wish to choose?For just counting
ip
s and ignoringid
andentry
altogether:For selecting a random row out of many that might be connected to the same
ip
(this is MySQL specific and would not work under other databases):For selecting all rows, together with
ip
counts:我相信您想要构建的结果/示例表有一些不那么合理的东西。您不能将 ID 和 ENTRY 与 IP 计数器结合起来。例如,结果/示例表中的第一行显示 IP
19.123.14.5
已被发现 2 次,这是正确的,但将其与 ID1
和 ENTRY 相关联abc
。为什么?您不将其与 ID2
和 ENTRYcde
关联的原因是什么。在 ID2
和 ENTRYefg
的行上还有另一个错误(?...我无法告诉...)。您提供的初始表中不存在此组合。包含所有 4 列的结果表什么也没说。除非您想始终显示找到的特定 IP 的第一个(最小)ID、ENTRY。是这样吗?
如果是这样,那么您的 SQL 不正确。正确的SQL是:
I believe that your result/example table that you want to build has something that is not that rational. You cannot combine ID and ENTRY with counter of IPs. For example, the first row in your result / example table says that IP
19.123.14.5
has been found 2 times, which is correct, but associates it to ID1
and ENTRYabc
. Why? What is the reason you do not associate it to ID2
and ENTRYcde
. There is also another mistake (? ... I cannot tell ...) on the row with ID2
and ENTRYefg
. This combination does not exist in the initial table that you give. The resulting table with all 4 columns says nothing.Unless you want to display always the first (minimum) ID, ENTRY that the particular IP was found. Is that so?
If this is so then your SQL is not correct. The correct SQL is: