Jump to content

Recommended Posts

Hi  :)

 

I am making a database of baseball players and their positions.  Each player usually has at least two positions.  I have two tables like this (simplified):

 

Players:

 

position1position2

31

23

12

 

 

Positions:

 

Position IDPosition

1First Base

2Second Base

3Third Base

 

I need to do a joins so that I can print both positions and not the position IDs, but I don't know how to do that when there are 3 columns?  I have had no luck so far.

 

Thanks!

-Tim

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/65715-solved-joining-3-columns/
Share on other sites

Can u post the sample result u r expecting ?

 

Sorry about that.  Here is a clearer version:

 

table name = players

 

player_idposition1position2

00352

00431

00523

 

table name = positions

 

position_idposition

11B

22B

33B

4SS

5OF

 

The result I want is:

 

player_idposition1position2

003OF2B

0043B1B

0052B3B

 

 

kirk112,

Can you explain what p1, p2, and py represent?  I have also tried to do two LEFT JOINs but have gotten an error message because you can't join the same table twice.  I haven't used the AS statement yet though...

 

 

 

The p1, p2, py are alias for the tables, you will need to use them if you want to join the same table twice.

 

py is the reference for the players table.

 

so the sql would be...

 

SELECT py.player_id, p1.position AS position1, p2.position AS position2

FROM players AS py

LEFT JOIN positions AS p1 ON py.position1 = p1.position_id

LEFT JOIN positions AS p2 ON py.position2 = p2.position_id

The p1, p2, py are alias for the tables, you will need to use them if you want to join the same table twice.

 

py is the reference for the players table.

 

so the sql would be...

 

SELECT py.player_id, p1.position AS position1, p2.position AS position2

FROM players AS py

LEFT JOIN positions AS p1 ON py.position1 = p1.position_id

LEFT JOIN positions AS p2 ON py.position2 = p2.position_id

 

That worked!

 

Thanks kirk!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.