[英]SQL Code for Joining Three tables and PHP
I have three tables that I want to be able to have a HTML select option that would display the name of the player and their primary and secondary positions that they are able to play. 我有三个表,我希望能够有一个HTML选择选项,显示播放器的名称及其能够播放的主要和次要位置。 The Player table only stores them as integers and not what their are known as.
Player表只将它们存储为整数而不是它们的所谓。 I created a lookup table that would convert the numbers to actual positions for example "C" = "1".
我创建了一个查找表,将数字转换为实际位置,例如“C”=“1”。 When I run the below SQL Query it does not display anything in my array.
当我运行下面的SQL Query时,它不会在我的数组中显示任何内容。
Player
Player_ID PrimaryPosition SecondaryPosition PlayerName
1 2 1 Bob
2 1 3 Billy
Team
Player_ID TeamID TeamName
1 1 Hobos
2 1 Rejects
PosLookup
PosNbr PosLabel PosLabel2
1 C C
2 P P
3 1B 1B
SELECT Player.PlayerName, PosLookup.PosLabel, PosLookup.PosLabel2, Player.Player_ID FROM Team, Player, PosLookup WHERE Player.Player_ID = Team.Player_ID and Team.TeamID = '1' and PosLookup.PosNbr = Player.PrimaryPosition and PosLookup.PosNbr = Player.SecondaryPosition ORDER BY PosLabel ASC
foreach($rowarray as $row)
{
echo "<option value = $row[PlayerID] >$row[PlayerName] $row[PosLabel] $row[PosLabel2]</option>";
$cntr++;
}
Desired OUTPUT Dropdown
Bob P C
Billy C 1B
Look at one of the conditions in your query: 查看查询中的一个条件:
PosLookup.PosNbr = Player.PrimaryPosition
AND PosLookup.PosNbr = Player.SecondaryPosition
So (eg Bob
) you're asking for rows where PosNbr
is 2 (Bob's primary position) AND PosNbr
is 1 (Bob's secondary position). 所以(例如
Bob
)你要求PosNbr
为2(Bob的主要位置)和PosNbr
为1(Bob的次要位置)的行。 This is not posible since a single PosNbr
can't be both 1 and 2. 这是不可能的,因为单个
PosNbr
不能同时为1和2。
Also, the PosLabel2
column is unnecessary in PosLookup
table (if it's just a clone of PosLabel
). 此外,
PosLookup
表中不需要PosLabel2
列(如果它只是PosLabel
的克隆)。
If you just want a table that is basically Player
with the labels substituted in for PrimaryPosition
and SecondaryPosition
and the name substituted in for Player_ID
(and filtered by TeamID
) try: 如果你只想要一个基本上是
Player
的表,其中标签被替换为PrimaryPosition
和SecondaryPosition
,而且名称替换为Player_ID
(并由TeamID
过滤),请尝试:
SELECT Player.Player_ID, Player.PlayerName, p1.PosLabel, p2.PosLabel AS PosLabel2
FROM Player
LEFT JOIN PosLookup p1 ON Player.PrimaryPosition=p1.PosNbr
LEFT JOIN PosLookup p2 ON Player.SecondaryPosition=p2.PosNbr
LEFT JOIN Team ON Team.Player_ID = Player.Player_ID
WHERE Team.TeamID=1;
+-----------+------------+----------+-----------+
| Player_ID | PlayerName | PosLabel | PosLabel2 |
+-----------+------------+----------+-----------+
| 1 | Bob | P | C |
| 2 | Billy | C | 1B |
+-----------+------------+----------+-----------+
Now use 现在用
echo "<option value = $row[Player_ID] >$row[PlayerName] $row[PosLabel] $row[PosLabel2]</option>";
Try this: 尝试这个:
SELECT
p.PlayerName, l1.PosLabel , l2.PosLabel
FROM
Player p, Team t
LEFT JOIN PosLookup l1 on p.PrimaryPosition = l1.PrimaryPosition,
LEFT JOIN PosLookup l2 on p.SecondaryPosition = l2.SecondaryPosition,
WHERE
p.TeamID = t.TeamID
t.TeamID = '1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.