[英]Joining 3 Tables via MySQL
I am trying to join 3 tables to access data from each table. 我试图加入3个表来访问每个表中的数据。 The main table in this case is going to be the Stats table.
在这种情况下,主表将是Stats表。 So I need to access the Players table to get their NextOpponent and then I need access to the Defense table to run calculations on the NextOpponent.
所以我需要访问Players表来获取他们的NextOpponent,然后我需要访问Defense表来运行NextOpponent上的计算。 Any ideas have to achieve all 3 of these.
任何想法都必须实现所有这3个。
Current SQL Statement: (Currently in an never ending loop of printing out duplicates) 当前的SQL语句:(目前在打印重复项的永无止境的循环中)
$query="SELECT * FROM Stats
INNER JOIN Defense ON Stats.Team = Defense.Team
INNER JOIN Players ON Defense.Team = Players.team";
Here is my database structure. 这是我的数据库结构。
Players
-PID (Primary)
-Name
-NextOpponent
Stats
-PID (Primary)
-Name
-Touchdowns
-Receptions
-etc....
Defense
-Team (Unique)
-Points
So my intended output would be: 所以我的预期输出是:
Chris Johnson
Next Opponent: IND
Defense Points: 100
UPDATE: I can now access the fields, however when I echo the defense points it actually returns the points of the teams defense the player is on not their next opponents defensive points. 更新:我现在可以访问这些领域,但是当我回应防守点时,它实际上返回了球队防守的分数,而不是他们的下一个对手的防守点。
Like this: 像这样:
SELECT * FROM Stats s, Defense d, Players p
WHERE s.Team = d.Team AND d.Team = p.team;
How about using GROUP BY p.id
? 如何使用
GROUP BY p.id
?
UPDATE UPDATE
This was the final query: 这是最后的查询:
SELECT * FROM Stats s JOIN Players p ON p.PID = s.PID JOIN Defense d ON d.Team = p.upcoming GROUP BY p.PID
SQL doesn't return endless data sets. SQL不返回无限的数据集。 I'd suggest looking at your code to ensure you read the next record inside your loop.
我建议查看你的代码,以确保你读取循环中的下一条记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.