SQL 查询 - 不同的结果
我有以下两个表:
People [*ID*, Name]
Pet [*PetID*, OwnerID, Species, Name]
(OwnerID 是 ID 的外键)
我希望数据库列出每个人以及他们拥有多少不同物种。例如,如果 Bob (ID 1473) 拥有一只狗、一只猫和另一只狗,则输出应该是:
ID | No. of Species
----------------------
1473 | 2
我意识到这需要相关子查询或外连接,但我不太确定如何做到这一点。任何帮助将不胜感激。
I have the following two tables:
People [*ID*, Name]
Pet [*PetID*, OwnerID, Species, Name]
(OwnerID is a foreign key of ID)
I would like the database to list each person and how many different species they own. For example, if Bob (ID 1473) owned a dog, cat and another dog the output should be:
ID | No. of Species
----------------------
1473 | 2
I realize that this would require correlated sub-queries or outer joins, but I'm not exactly sure how to do that. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
count(distinct ...)
来实现:You could use
count(distinct ...)
for that:试试这个
try this