我有一张表users
id, ip, opponent
1, 123.123.123, 2
2, 123.123.123, 1
3, 123.123.123, NULL
我需要查询,向我显示没有重复的ips列表。
ip_first_opponent, ip_second_opponent
我怎么能这样做,我是sql中的新手。
答案 0 :(得分:0)
如果您希望获得id为x的用户的所有ips,您可以执行以下操作:
SELECT DISTINCT(opponents.id)FROM users as me,users as opponents WHERE me.id = x and me.opponentId = opponents.id
(所以你加入表用户自己的约束,对手id与对手的id相同,而DISTINCT只输出不同的ips。)
编辑: 由于您要选择用户及其对手的IP,查询将变为:
SELECT DISTINCT me.ip,opponents.ip FROM users AS me,users as opponents WHERE me.opponentId = opponents.id