对彼此关系不确定的条目进行计数
我想计算数据库中所有与一个人都有指定关系的条目列表,但我需要根据它们彼此之间的关系进行计数 - 换句话说,他们被列为家里主要人物的儿子和女儿、兄弟姐妹——我需要计算兄弟姐妹的数量。
我的方法是尝试统计所有有一个以上儿子或一个女儿的家庭,或者有一个儿子和一个女儿的所有家庭,或者有一个兄弟和一个姐妹、一个以上兄弟或一个以上姐妹的所有家庭。然而,当我尝试将其放入 SQL 中时,我一无所获。
SELECT FirstName, Surname
FROM individuals
WHERE (RelationshipToHead = 'Son' AND RelationshipToHead = 'Daughter')
OR (RelationshipToHead = 'Son' AND RelationshipToHead = 'Son')
OR (RelationshipToHead = 'Daughter' AND RelationshipToHead = 'Daughter')
OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Sister')
OR (RelationshipToHead = 'Sister' AND RelationshipToHead = 'Sister')
OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Brother')
这仅返回包含儿子、女儿、兄弟或姐妹的所有条目,而不是每个家庭中每个条目都有一个以上的条目。
您能建议一个更好的查询吗?
I want to count a list of entries in my database that all have specified relationships to one person, but I need to count by their relationship to each other -
in other words, they are listed as sons and daughters, brothers and sisters of the main person in the house - I need to count the number of siblings.
My approach was to try to count all the households with more than one son or daughter or any household with a son and a daughter or any household with a brother and a sister, more than one brother or more than one sister. However when I try to put this into SQL I'm not getting anywhere.
SELECT FirstName, Surname
FROM individuals
WHERE (RelationshipToHead = 'Son' AND RelationshipToHead = 'Daughter')
OR (RelationshipToHead = 'Son' AND RelationshipToHead = 'Son')
OR (RelationshipToHead = 'Daughter' AND RelationshipToHead = 'Daughter')
OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Sister')
OR (RelationshipToHead = 'Sister' AND RelationshipToHead = 'Sister')
OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Brother')
This merely returns all entries that contain son daughter brother or sister - not the ones with more than one of each in each family.
Could you suggest a better query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
where
子句仅比较每行中的值,这就是您的查询未按您想要的方式运行的原因。我建议:The
where
clause only compares values within each row, which is why your query isn't behaving as you want. I suggest: