Got 3 Tables:pers,skills,articles(人有n个技能,写了n篇文章)
(T1) 个人
1 John
2 Joe
(T2) 技能
1 John_sings
1 John_laughs
2 Joe_runs
(T3) 文章
1 John_article_1
2 Joe_article_1
3 Joe_article_2
我预计:
John - John_laughs - John_article_1
John - John_sings - [NULL]
Joe - Joe_runs - Joe_article_1
Joe - [NULL] - Joe_article_2
因为我们有 2 个单独的 1:n 关系,所以根据这个问题,连续连接不会这样做 -> 不是 T1 x T2 x T3,而是 (T1 x T2) x (T1 x T3) 。
我试过了:
SELECT child1.id,
child1.name,
child1.skill,
child2.title
FROM
(SELECT pers.id,
pers.name,
skills.skill
FROM pers
LEFT JOIN skills ON pers.id = skills.pers_id) child1
INNER JOIN
(SELECT pers.id,
article.title
FROM pers
LEFT JOIN article ON pers.id = article.pers_id) child2 ON child1.id = child2.id
但这表明
John - John_laughs - John_article_1
John - John_sings - John_article_1
Joe - Joe_runs - Joe_article_1
Joe - Joe_runs - Joe_article_2
显然,我不想要两次“Joe_runs”,也不想两次“John_article_1”。
感谢任何建议!