在本示例中,如何从另一列中选择匹配的数据:
id user_id match_id
1 3 4
2 3 5
3 4 3
4 4 6
注意:ID列是自动增量列
所以基本上输出应该是 user_id 3 和 user_id 4 匹配,因为 user_id 3 在 match_id 列中具有 user_id 4,而 user_id 4 在 match_id 列中具有 user_id 3
最佳答案
假设您列出的表是 user_matching
,并且您尝试加入的表是 users
并且它都有一个 id
和 username
列,您可以使用以下内容(使用 u.
和 u2.
来区分匹配的用户/成员上的字段,或者原始用户/成员(member)):
SELECT u.username, u2.username AS match_username
FROM user_matching m
LEFT JOIN users u ON u.id = u.user_id
LEFT JOIN users u2 on u2.id = u.match_id
关于php - MYSQL从另一列找到匹配的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417184/