doujianglin6704 2012-05-05 19:16
浏览 134
已采纳

MySQL Query将查找用户使用相同IP地址的多个用户名

I've done some homework on this issue and I am pretty sure it will involve joining a table with itself but I'm not quite there yet.

I have a forum with a "posts" table. It stores the ipaddress, username, userid, and postid for every post made on the forum.

I am trying to get a query I can run each week or so that will output me a list of ipaddresses that have been used by multiple userids during this time- and the postids involved where the same ipaddress was used by multiple userids.

I've got this so far from other people's similar questions here:

SELECT t1.userid,t2.userid, t1.username, t2.username, t1.ipaddress, t2.ipaddress, t1.postid, t2.postid
FROM qvb_post t1
INNER JOIN qvb_post t2 ON t1.ipaddress=t2.ipaddress AND t1.userid!=t2.userid
WHERE 
t1.dateline > 1335897698 AND t2.dateline > 1335897698

But it returns a lot of unnecessary rows IMO, fr example it returns each duplicate both ways- both as if userid 1 used userid 2's ipaddress and again as if userid2 used userid1's address.

The dateline is from a few days ago.

Ideally I'd like to just see a list of ip addresses in order with the duplicate usernames and repeated for each post, so I can go in with PHP and make a report that outputs an IP Address, the usernames (userids) that used it, and the postids created by them.

I am aware that there are many valid reasons two people will have the same ip address, rest assured no harsh action would be taken against anyone merely for showing up in this report.

Thank you in advance for any help you can provide.

Just to be clear I'm fine with the PHP, it's only the Query I need help with.

  • 写回答

1条回答 默认 最新

  • dozc58418381 2012-05-05 19:22
    关注

    This should give you a start:

    SELECT ipaddress, GROUP_CONCAT(DISTINCT username)
    FROM qvb_post
    GROUP BY ipaddress
    HAVING COUNT(DISTINCT username) > 1
    ORDER BY COUNT(DISTINCT username) DESC
    

    It shows you IP addresses used by more than one distinct user name, and also gives you a comma separated list of those user names for each IP address. You could extend it to also include the post IDs if you wish.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路