Mysql 要联接的同一列的SQL表

Mysql 要联接的同一列的SQL表,mysql,sql,join,Mysql,Sql,Join,我目前有一个问题,即如何在同一个表中单独获取数据,但条件不同 为了更好地说明,请将下面的示例作为我的表格 争议表 id | user_to | bidder_id 1 | 1 | 2 user_id | user_name 1 | userone 2 | usertwo id | user_to | bidder_id | user_to_name | bidder_id_name 1 | 1 | 2 | userone

我目前有一个问题,即如何在同一个表中单独获取数据,但条件不同

为了更好地说明,请将下面的示例作为我的表格

争议表

id | user_to | bidder_id
1  | 1       | 2
user_id | user_name
1       | userone
2       | usertwo
id | user_to | bidder_id | user_to_name  | bidder_id_name
1  | 1       | 2         | userone       | usertwo
用户表

id | user_to | bidder_id
1  | 1       | 2
user_id | user_name
1       | userone
2       | usertwo
id | user_to | bidder_id | user_to_name  | bidder_id_name
1  | 1       | 2         | userone       | usertwo
我希望有一个这样组合两者的输出:

最终输出表

id | user_to | bidder_id
1  | 1       | 2
user_id | user_name
1       | userone
2       | usertwo
id | user_to | bidder_id | user_to_name  | bidder_id_name
1  | 1       | 2         | userone       | usertwo
我不知道如何用语言来表达,但我希望这个例子能有所帮助:


它寻找“用户标识”和“投标人标识”行,将它们与用户表中的“用户标识”相关联,在用户表中创建两个新列,将“用户标识”和“投标人标识”与用户表中的相应标识相关联,并在字段中给定的标识中获取用户名。

LEFT JOIN是您的朋友。请参见exsample:

样本

SELECT d.*, 
 utn.user_name AS user_to_name , 
 bin.user_name AS bidder_id_name
FROM disputes d
LEFT JOIN users utn on utn.user_id = d.user_to
LEFT JOIN users bin on bin.user_id = d.bidder_id;