多个连接在同一个表中有两个外键返回零结果

时间:2018-05-25 03:38:08

标签: mysql sql

我有两张桌子,我想一起加入两张桌子。

(tbl_user)

id | user_name
6  | 11223  
5  | 3322
7  | 5566
8  | 7788

(tbl_friends)

我将这两列合并为唯一的

user_one_id | user_two_id
5|8
5|6

查询:

SELECT user.user_name
FROM tbl_user user
INNER JOIN tbl_friends a
ON user.user_id = a.user_one_id
INNER JOIN tbl_friends b
ON user.user_id = b.user_two_id;

2 个答案:

答案 0 :(得分:0)

正确的想法,错误的实施。也许你想要:

SELECT u1.user_name as name1, u2.user_name as name2
FROM tbl_friends f JOIN
     tbl_users u1
     ON u1.user_id = f.user_one_id JOIN
     tbl_users u2
     ON u2.user_id = f.user_two_id;

答案 1 :(得分:0)

请执行以下查询

SELECT u1.user_name name1, u2.user_name name2
FROM tbl_friends fn
        Join
     tbl_users u1
     ON u1.id = fn.user_one_id JOIN
     tbl_users u2
     ON u2.id = fn.user_two_id;