MySQL选择记录,计算重复项

发布于 2024-12-11 18:15:10 字数 1698 浏览 0 评论 0原文

我有一个帖子表,每个帖子都有一个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 技术交流群。

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

发布评论

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

评论(4

眼睛会笑 2024-12-18 18:15:10
SELECT ip, min(entry), max(id) as id, COUNT(ip) AS A 
FROM table_name AS C 
GROUP BY ip
ORDER BY max(id) DESC

由于您想按 IP 和条目进行分组,因此您缺少执行 MAX(id) 操作。您可以删除 Having>1 因为您希望根据示例输出看到 counts =1

示例脚本:

declare @table table
(
id int,
entry varchar(20),
ip varchar(20)
)
    insert into @table
    values

(1,   'abc',  '19.123.14.5'),
(2,   'cde' , '19.123.14.5'),
(3 ,  'efg',  '12.231.22.9')


SELECT  max(id) as id, ip, max(entry), COUNT(ip) AS count 
FROM @table AS C 
GROUP BY ip
ORDER BY max(id) asc

生成:

id          ip                                        count
----------- -------------------- -------------------- -----------
2           19.123.14.5          cde                  2
3           12.231.22.9          efg                  1
SELECT ip, min(entry), max(id) as id, COUNT(ip) AS A 
FROM table_name AS C 
GROUP BY ip
ORDER BY max(id) DESC

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 output

Sample script:

declare @table table
(
id int,
entry varchar(20),
ip varchar(20)
)
    insert into @table
    values

(1,   'abc',  '19.123.14.5'),
(2,   'cde' , '19.123.14.5'),
(3 ,  'efg',  '12.231.22.9')


SELECT  max(id) as id, ip, max(entry), COUNT(ip) AS count 
FROM @table AS C 
GROUP BY ip
ORDER BY max(id) asc

Produces:

id          ip                                        count
----------- -------------------- -------------------- -----------
2           19.123.14.5          cde                  2
3           12.231.22.9          efg                  1
三五鸿雁 2024-12-18 18:15:10

您的 ip=19.123.14.5 有不同的 identries。您想选择其中哪一位?

仅计算 ip 并忽略 identry

SELECT ip, COUNT(*)
FROM table_name
GROUP BY ip

12.231.22.9, 1
19.123.14.5, 2

用于从可能连接到同一 的许多行中选择随机行>ip (这是 MySQL 特定的,在其他数据库下不起作用):

SELECT id, entry, ip, COUNT(*)
FROM table_name
GROUP BY ip

3, efg, 12.231.22.9, 1
1, abc, 19.123.14.5, 2 // Might also be: 2, cde, 19.123.14.5, 2

用于选择所有行以及 ip 计数:

SELECT *, (SELECT COUNT(*) FROM table_name t2 WHERE t1.ip = t2.ip)
FROM
    table_name t1

1, abc, 19.123.14.5, 2
2, cde, 19.123.14.5, 2
3, efg, 12.231.22.9, 1

You have different ids and entries for ip=19.123.14.5. Which one of them you wish to choose?

For just counting ips and ignoring id and entry altogether:

SELECT ip, COUNT(*)
FROM table_name
GROUP BY ip

12.231.22.9, 1
19.123.14.5, 2

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):

SELECT id, entry, ip, COUNT(*)
FROM table_name
GROUP BY ip

3, efg, 12.231.22.9, 1
1, abc, 19.123.14.5, 2 // Might also be: 2, cde, 19.123.14.5, 2

For selecting all rows, together with ip counts:

SELECT *, (SELECT COUNT(*) FROM table_name t2 WHERE t1.ip = t2.ip)
FROM
    table_name t1

1, abc, 19.123.14.5, 2
2, cde, 19.123.14.5, 2
3, efg, 12.231.22.9, 1
凉薄对峙 2024-12-18 18:15:10
SELECT t.id, t.entry, t.ip, q.IpCount 
    FROM (SELECT ip, MIN(id) AS MinId, COUNT(*) AS IpCount
              FROM table_name
              GROUP BY ip) q
        INNER JOIN table_name t
            ON q.ip = t.ip
                AND q.MinId = t.id
    ORDER BY t.id DESC;
SELECT t.id, t.entry, t.ip, q.IpCount 
    FROM (SELECT ip, MIN(id) AS MinId, COUNT(*) AS IpCount
              FROM table_name
              GROUP BY ip) q
        INNER JOIN table_name t
            ON q.ip = t.ip
                AND q.MinId = t.id
    ORDER BY t.id DESC;
情深缘浅 2024-12-18 18:15:10

我相信您想要构建的结果/示例表有一些不那么合理的东西。您不能将 ID 和 ENTRY 与 IP 计数器结合起来。例如,结果/示例表中的第一行显示 IP 19.123.14.5 已被发现 2 次,这是正确的,但将其与 ID 1 和 ENTRY 相关联abc。为什么?您不将其与 ID 2 和 ENTRY cde 关联的原因是什么。在 ID 2 和 ENTRY efg 的行上还有另一个错误(?...我无法告诉...)。您提供的初始表中不存在此组合。包含所有 4 列的结果表什么也没说。

除非您想始终显示找到的特定 IP 的第一个(最小)ID、ENTRY。是这样吗?

如果是这样,那么您的 SQL 不正确。正确的SQL是:

 select aa.min_id as id, 
           b.entry, 
           aa.ip as ip, 
           aa.count_ip as count     
 from table_name b     
 join (select min(a.id) as min_id, 
                 a.ip, 
                 count(a.ip) as count_ip 
          from table_name a 
          group by a.ip) aa 
      on aa.min_id = b.id;

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 ID 1 and ENTRY abc. Why? What is the reason you do not associate it to ID 2 and ENTRY cde. There is also another mistake (? ... I cannot tell ...) on the row with ID 2 and ENTRY efg. 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:

 select aa.min_id as id, 
           b.entry, 
           aa.ip as ip, 
           aa.count_ip as count     
 from table_name b     
 join (select min(a.id) as min_id, 
                 a.ip, 
                 count(a.ip) as count_ip 
          from table_name a 
          group by a.ip) aa 
      on aa.min_id = b.id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文