如何根据多个表选择SQL结果
我需要根据其他几个表中的某些匹配值从一个表中选择结果。我有下表:
person: id, firstname, lastname
team: id, teamname
player: id, person_id(FK), team_id(FK)
coach: id, person_id(FK), team_id(FK)
我需要返回每支球队的所有教练和球员姓名。我只使用过内部联接,而且似乎我不能在这里使用它们,所以知道如何做到这一点吗?
I need to select results from one table based on certain matching values in a couple of other tables. I have the following tables:
person: id, firstname, lastname
team: id, teamname
player: id, person_id(FK), team_id(FK)
coach: id, person_id(FK), team_id(FK)
I need to return all the coaches and players names for each team. I've only ever used inner joins, and it doesn't seem like I can use those here, so any idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将为您提供教练:
这将为您提供球员:
因此,不优雅、简单的答案是将它们与
UNION
一起扔掉。This will give you the coach:
And this will give you the players:
So, the non-elegant, simple answer is to just toss it all together with
UNION
.只需在加入团队时使用 OR
或者如果您愿意并且您的数据库具有 COALESCE
Just use an OR in the join to Team
Or if you perfer if and your database has COALESCE