如果我想选择明确的用户(即WHERE username ='peter'),那么SQL SELECT查询应如何表示GROUP所有行BY ip和SELECT所有其他具有相同IP的用户?
这是表格的内容,包含字段:id,ip,username
1 | 192.0.0.1 |彼得
2 | 192.0.0.1 |插孔
3 | 192.0.0.2 |彼得
4 | 192.0.0.3 |贾斯汀
5 | 192.0.0.2 |贾斯汀
执行必要的SQL SELECT查询应该像这样选择(使用WHERE username ='peter'):
2 | 192.0.0.1 |插孔
5 | 192.0.0.2 |贾斯汀
这是选择了用户名行的SELECT查询,这些用户名与目标用户名'peter'具有相同的IP。
任何人都可以帮我吗?
提前谢谢!
答案 0 :(得分:2)
SELECT * FROM your_table WHERE ip_address IN (SELECT ip_address FROM your_table WHERE user_name = 'peter');
答案 1 :(得分:2)
SELECT *
FROM your_table
WHERE ip_address IN (SELECT ip_address
FROM your_table
WHERE user_name = 'peter')
AND user_name <> 'peter'
答案 2 :(得分:0)
SELECT t1.*
FROM iptable t1
INNER JOIN iptable t2 ON t1.ip = t2.ip AND t1.username <> t2.username
WHERE t2.username = 'peter'