以下查询:
SELECT friendship.dom, friendship.sub,
user.id, user.fname, user.lname
FROM friendship
LEFT JOIN user ON friendship.dom = user.id
导致以下结果:
dom sub id fname lname
---------------------------
2 1 2 Seun Soti
1 2 1 Donal Lynch
但是如何使输出看起来像:
dom sub id_dom fname_dom lname_dom id_sub fname_sub lname_sub
----------------------------------------------------------------------
2 1 2 Seun Soti 1 Donal Lynch
1 2 1 Donal Lynch 2 Seun Soti
我只需要将两个用户(dom
和sub
)都包含在输出中。
答案 0 :(得分:2)
这个怎么样:
SELECT
f.dom, f.sub,
d.id dom_id, d.fname dom_fname, d.lname dom_lname,
s.id sub_id, s.fname sub_fname, s.lname sub_lname
FROM friendship f
LEFT JOIN user d ON f.dom = d.id
LEFT JOIN user s ON f.sub = s.id
答案 1 :(得分:1)
试试这个:
SELECT friendship.dom, friendship.sub, user.id AS id_dom, ...
FROM friendship
LEFT JOIN user ON friendship.dom = user.id
这对你有用。 HTH
答案 2 :(得分:1)
SELECT friendship.dom, friendship.sub,
user.id, user.fname, user.lname,
u1.id, u1.fname, u1.lname
FROM friendship
FROM friendship
LEFT JOIN user ON friendship.dom = user.id
LEFT JOIN user as u1 ON friendship.sub = u1.id