Jump to content

Mysql SELECT ip address problem


andy75180

Recommended Posts

Hi ya,

 

Got a problem I'm trying to solve with retrieving data from my mysql database. Wondered if anyone could help.

 

Have a login system with my chess site that logs each player's username and ip when they login to play. I need a query that select only entries that have an IP address associated with 2 or more usernames. This will help me track down cheating users that have 2 or more accounts.

 

Here's an example table:-

 

--USERNAME-----IP ADDRESS---

 

--terry-------123.123.123.123--

--tony-------65.33.22.43-------

--larry-------185.43.5.67-------

--larry-------185.43.5.67-------

--paul--------32.23.0.4--------

--andy-------123.123.123.123--

--zippy-------55.56.34.123-----

 

The correct mysql select query would retrieve the ip address 123.123.123.123 because terry and andy are using it. 185.43.5.67 would not be retrieved because only larry is using it which is okay.

 

Any ideas anyone on the select query I need to submit?

 

 

 

 

 

Link to comment
Share on other sites

Hi

 

My attempt.

 

SELECT DISTINCT USERNAME
FROM TestUserIp
WHERE ipaddress
IN (

SELECT DISTINCT ipaddress
FROM TestUserIp a
WHERE (

SELECT count( * ) AS n
FROM TestUserIp b
WHERE b.ipaddress = a.ipaddress
) >=2
)

 

This will give you a list of the user names who have used ip addresses used by more than one user

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Mchls way of doing it is nice and tidy and I like it, although I think he missed a where clause:-

 

SELECT * FROM table WHERE ip IN (
SELECT ip FROM table AS t1 CROSS JOIN table AS t2 USING (ip) WHERE t1.username != t2.username AND t1.ip = t2.ip GROUP BY ip)

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.