MySQL选择记录,计算重复数

时间:2011-10-25 19:15:08

标签: php mysql count

我有一个帖子表,每个帖子都有一个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

4 个答案:

答案 0 :(得分:2)

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,因为您希望根据样本输出看到计数= 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

答案 1 :(得分:1)

id的{​​{1}}和entries不同。你想选择哪一个?

仅计算ip=19.123.14.5并完全忽略ipid

entry

用于选择可能连接到同一SELECT ip, COUNT(*) FROM table_name GROUP BY ip 12.231.22.9, 1 19.123.14.5, 2 的许多随机行(这是MySQL特定的,在其他数据库下不起作用):

ip

选择所有行以及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

答案 2 :(得分:0)

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;

答案 3 :(得分:0)

我相信你想要构建的结果/示例表有一些不合理的东西。您不能将ID和ENTRY与IP计数器结合使用。例如,结果/示例表中的第一行表示已找到IP 19.123.14.5 2次,这是正确的,但会将其与ID 1和ENTRY abc相关联。为什么?您不将其与ID 2和ENTRY cde相关联的原因是什么。在ID 2和ENTRY efg的行上还有另一个错误(?...我说不出来......)。您提供的初始表中不存在此组合。所有4列的结果表都没有说明。

除非您想要始终显示第一个(最小)ID,否则找到特定IP的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;