开发者

Finding Alternative Accounts, SQL

I have a setup like this:

Username | IP
Foo      | 1.2.3.4
Bar      | 1.2.3.4
Baz      | 1.2.3.6

Say I开发者_如何学运维 want to find alternative accounts of the user "Foo". Something like this should return:

Username | IP
Bar      | 1.2.3.4

How would I do such a thing in one SQL query?


A self join should do the trick

this will give you all users that have more than one

Select * from 
Table t1
INNER JOIN Table t2
ON t1.ip = t2.ip
and t1.Username  <> t2.Username 

you can adjust the where as you would want

e.g.

Where
 t1.UserName = 'Foo'


SELECT * FROM TableName
 LEFT JOIN TableName AS TableName2 
 ON TableName2.IP = TableName.IP AND TableName2.Username != TableName.Username
 WHERE TableName.Username = 'Foo'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜