I have the following two MySQL tables which I need to join:
A (donor) B (documents)
============ =============================
id | name id | profile_img | ref_id
------------ -----------------------------
1 | Alex 1 | a.png | 1
2 | Bill 2 | b.jpg | 2
3 | Cath 3 | c.jpg | 3
4 | Dale 4 | d.jpg | 4
5 | Evan
6 | Test
7 | Test2
SELECT d.name, d.id, c.name, c.ref_id FROM donor d, documents c WHERE
ref_id = d.id
问题是只有那些记录是获取的,而捐赠者表休息中没有显示。我希望在找不到右侧匹配时始终显示数据
答案 0 :(得分:0)
你需要一个LEFT JOIN
。它可以通过加号+
完成您的语法(不应该使用!)。我建议你只使用显式类型的连接:
SELECT d.name, d.id, COALESCE(c.name,'DefaultVal') , COALESCE(c.ref_id,'DefaultVal')
FROM donor d
LEFT JOIN documents c
ON(ref_id = d.id)
答案 1 :(得分:0)
使用LEFT JOIN
SELECT d.name, d.id, c.name, c.ref_id FROM donor d LEFT JOIN documents c ON
d.id = c.id WHERE c.ref_id=d.id