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 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了