简体   繁体   中英

MySQL Query that will find users using the same IP Address for multiple usernames

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM