我有一张家庭和宠物名称表:
TABLE A: Families
Smith
Jones
Chan
TABLE B: Pet names
Socks
Spot
Precious
他们就像这样联系在一起:
Table C: Family Dogs (families <-> pet names)
Smith Socks
Jones Spot
Table D: Family Cats (families <-> pet names)
Jones Socks
Chan Precious
我想要的输出是:
Family Dog Cat
-------------------------
Smith Socks NULL
Jones Spot Socks
Chan NULL Precious
这看起来很简单,但我尝试了几种外连接组合,似乎没有产生输出。
答案 0 :(得分:0)
在此示例中,“宠物名称”表中不包含“家庭犬”和“家庭猫”表中尚未包含的任何信息,因此将“家庭”表加入两个链接就足够了表:
SELECT Families.Name AS Family,
"Family Dogs".Pet AS Dog,
"Family Cats".Pet AS Cat
FROM Families
LEFT JOIN "Family Dogs" ON Families.Name = "Family Dogs".Family
LEFT JOIN "Family Cats" ON Families.Name = "Family Cats".Family
如果一个家庭有一只以上的狗/猫,这将会爆发。