似乎无法解决这个问题。我有一个名为Person的数据文件,其中包含父母(母亲和父亲)的记录。到目前为止,使用下面的代码,我可以返回包含pupil和father数据的一行数据。但是,我还需要将母数据包含在同一行中,即使未找到父数据,反之亦然。我正在调查UNION的查询,复制我母亲的下面的内容(chenage fatherrelation to motherrelation等)并尝试将结果组合在一起,但我想知道是否有更简单有效的方法来做我想做的事情要做,这就是母亲的身份。标题,母亲。姓名和母亲姓名字段。
以下作品但仅返回父亲姓名。我也需要得到母亲的名字
SELECT DISTINCT(family.pk_FamilyID), family.Salutation, pupil.Title, pupil.Forename, pupil.Surname, father.Title, father.Forename, father.Surname, mother.Title, mother.Forename, mother.Surname
FROM Family
INNER JOIN PersonFamily ON family.pk_FamilyID = personfamily.fk_FamilyID
INNER JOIN Person father ON personfamily.fk_PersonID = father.pk_PersonID
INNER JOIN Relation fatherrelation ON father.pk_PersonID = fatherrelation.fk_RelatedPersonID
INNER JOIN Person pupil ON fatherrelation.fk_PersonID = pupil.pk_PersonID
WHERE fatherrelation.Relationship LIKE '%Father'
AND pupil.pk_PersonID IN('" & Substitute ( ListOfUUID ; ¶ ; "','" ) & "')
ORDER BY pupil.SurnameForename ASC
试图没有联盟 - 我认为你可以在内部联接中使用OR
SELECT DISTINCT(family.pk_FamilyID), family.Salutation, pupil.Title, pupil.Forename, pupil.Surname, father.Title, father.Forename, father.Surname, mother.Title, mother.Forename, mother.Surname
FROM Family
INNER JOIN PersonFamily ON family.pk_FamilyID = personfamily.fk_FamilyID
INNER JOIN Person father ON personfamily.fk_PersonID = father.pk_PersonID
INNER JOIN Person mother ON personfamily.fk_PersonID = mother.pk_PersonID
INNER JOIN Relation fatherrelation ON father.pk_PersonID = fatherrelation.fk_RelatedPersonID
INNER JOIN Relation motherrelation ON mother.pk_PersonID = motherrelation.fk_RelatedPersonID
INNER JOIN Person pupil ON fatherrelation.fk_PersonID = pupil.pk_PersonID
OR motherrelation.fk_PersonID = pupil.pk_PersonID
WHERE fatherrelation.Relationship LIKE '%Father'
OR motherrelation.Relationship LIKE '%Mother'
AND pupil.pk_PersonID IN('" & Substitute ( ListOfUUID ; ¶ ; "','" ) & "')
ORDER BY pupil.SurnameForename ASC
答案 0 :(得分:0)
您需要再次加入Person。而且我把你的学生变成你选择的根,因为这就是你有PK的。
SELECT family.pk_FamilyID, family.Salutation, pupil.Title, pupil.Forename, pupil.Surname, father.Title, father.Forename, father.Surname, mother.Title, mother.Forename, mother.Surname
FROM Person pupil
INNER JOIN PersonFamily pupilFamily on pupilFamily.fk_person_id = pupil.pk_person_id
INNER JOIN Family on family.pk_family_id = pupilFamily.fk_family_id
INNER JOIN PersonFamily ON family.pk_FamilyID = personfamily.fk_FamilyID
LEFT OUTER JOIN Person father ON personfamily.fk_PersonID = father.pk_PersonID
LEFT OUTER JOIN Person mother ON personfamily.fk_PersonID = mother.pk_PersonID
LEFT OUTER JOIN Relation fatherrelation ON father.pk_PersonID = fatherrelation.fk_RelatedPersonID
AND fatherrelation.Relationship like '%Father'
LEFT OUTER JOIN Relation motherrelation ON mother.pk_PersonID = motherrelation.fk_RelatedPersonID
AND motherrelation.Relationship like '%Mother'
WHERE pupil.pk_PersonID IN('" & Substitute ( ListOfUUID ; ¶ ; "','" ) & "')
ORDER BY pupil.SurnameForename ASC