简体   繁体   English

MySQL查询两次连接同一张表,没有重复

[英]MySQL query join this same table twice without duplicates

I have a table users 我有一个表users

id, ip, opponent
1, 123.123.123, 2
2, 123.123.123, 1
3, 123.123.123, NULL

I need query that, show me list of versus ips without duplicates. 我需要查询,向我显示没有重复的vs ip列表。

ip_first_opponent, ip_second_opponent

How can I do it, i am newbie in sql. 我该怎么做,我是sql的新手。

In case you want to get all the ips of the opponents of the user with id x, you can do the following: 如果要获取ID为x的用户的对手的所有ip,可以执行以下操作:

SELECT DISTINCT(opponents.id) FROM users AS me, users AS opponents WHERE me.id=x and me.opponentId=opponents.id 从用户AS我,用户AS对手中选择DISTINCT(opponents.id),其中me.id = x和me.opponentId = opponents.id

(So you join the table users to itself with the constraint that the opponent id is the same as the id of the opponent and the DISTINCT only outputs different ips.) (因此,您将表用户加入自己的约束是对手的ID与对手的ID相同,而DISTINCT仅输出不同的ip。)

Edit: Since you want to select the ip of the user and his opponents, the query becomes: 编辑:由于您要选择用户及其对手的IP,因此查询变为:

SELECT DISTINCT me.ip, opponents.ip FROM users AS me, users AS opponents WHERE me.opponentId=opponents.id 从用户AS我,用户AS对手中选择DISTINCT me.ip,kernels.ip WHERE me.opponentId = opponents.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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