punkdis
punkdis

Reputation: 43

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. The Player table only stores them as integers and not what their are known as. I created a lookup table that would convert the numbers to actual positions for example "C" = "1". When I run the below SQL Query it does not display anything in my array.

  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

Upvotes: 0

Views: 280

Answers (2)

mathematical.coffee
mathematical.coffee

Reputation: 56935

Look at one of the conditions in your query:

PosLookup.PosNbr = Player.PrimaryPosition 
AND PosLookup.PosNbr = Player.SecondaryPosition

So (e.g. Bob) you're asking for rows where PosNbr is 2 (Bob's primary position) AND PosNbr is 1 (Bob's secondary position). This is not posible since a single PosNbr can't be both 1 and 2.

Also, the PosLabel2 column is unnecessary in PosLookup table (if it's just a clone of 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:

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>";

Upvotes: 2

rkosegi
rkosegi

Reputation: 14678

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'

Upvotes: 0

Related Questions