简体   繁体   English

SQL-无法弄清楚如何联接三个表

[英]SQL - Can't figure out how to join three tables

I have four tables like this: 我有四个这样的表:

       **USERS**
___________________________
user_ID  username  password
---------------------------
1        user1     1234
2        user2     5678


              **TEAMS**
______________________________________  
team_ID  formation  team_name  user_ID
--------------------------------------
1        4-4-2      team1      1
2        4-3-3      team2      2

            **PLAYERS** 
____________________________________    
player_ID  name     position  rating
------------------------------------
1          Ronaldo  LW        94
2          Messi    RW        93
3          Hazard   LW        90

         **ACTIVE PLAYERS** 
___________________________________
ID  player_ID  team_ID  cardview_ID
-----------------------------------
1   1          2        9
2   3          1        7
3   2          1        3  

Each user has a team with a formation and a team name. 每个用户都有一个带有编队和团队名称的团队。 The "active players" tables references the player_ID with the team_ID to see which players are currently active on which teams. “活跃玩家”表将玩家player_IDteam_ID引用,以查看哪些玩家当前在哪些团队中处于活跃状态。

Let's say that user1 logs in to the application, then I want to get all the players name , rating and their cardview_ID . 假设user1登录到该应用程序,然后我想要获取所有玩家的namerating和他们的cardview_ID Something that should look like this: 应该是这样的:

_____________________________
name    rating    cardview_ID
-----------------------------
Hazard  90        7
Messi   94        3

These are the players that are currently active on user1's team which is team1 . 这些是当前在user1's团队team1上活跃的玩家。

How can I get this joined table? 如何获得此联接表? I have tried with an inner join but that didn't seem to do the work for me. 我尝试使用inner join但这似乎并没有为我完成工作。

_______________________________ EDIT_____________________________________ _______________________________编辑_____________________________________

This is the query that doesn't give the desired result: 这是没有给出期望结果的查询:

SELECT players.name, players.rating, activeplayers.cardview_ID
FROM players
INNER JOIN
activeplayers
ON players.player_ID = usedplayers.player_ID

I also tried to join them on team_ID. 我还尝试通过team_ID加入他们。

Assuming you have the logged in user's ID available, I think this will give you what you're asking for: 假设您具有可用的登录用户ID,我认为这将为您提供所需的信息:

SELECT
    [PLAYERS].name,
    [PLAYERS].rating,
    [ACTIVE PLAYERS].cardview_ID
FROM [TEAMS]
JOIN [ACTIVE PLAYERS]
ON [TEAMS].team_ID = [ACTIVE PLAYERS].team_id
JOIN [PLAYERS]
ON [PLAYERS].player_id = [ACTIVE PLAYERS].player_id
WHERE [TEAMS].user_id = <logged_in_user_id>

Please also note the questions asking for clarifying details, and also feel free to respond if this query gets you part of the way but you need more information. 另请注意一些问题,要求您澄清详细信息,如果此查询使您成为问题的一部分,但是您需要更多信息,请随时回答。 The content in angle brackets are of course a placeholder. 尖括号中的内容当然是占位符。 I also don't know your exact table names so you may need to replace what is in the square brackets with the actual table names. 我也不知道您的确切表名,因此您可能需要用实际的表名替换方括号中的内容。

Assuming that the query in your post contains a typo and is actually this: 假设您帖子中的查询包含错字,实际上是这样的:

SELECT players.name, players.rating, activeplayers.cardview_ID
FROM players
INNER JOIN
activeplayers
ON players.player_ID = activeplayers.player_ID

This query will correctly return all the players who are active players. 此查询将正确返回所有活跃玩家。 Now to limit it only to the team for User1, you need to add an additional join to the Teams table, same way you did the join above, and then add a WHERE clause that filters on the Teams.UserID . 现在,仅将其限制为User1的团队,您需要以与上述连接相同的方式向Teams表添加一个附加Teams.UserID ,然后添加对Teams.UserID过滤的WHERE子句。

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM