0

I have two tables, Table A has two foreign key columns (div_player1_id, div_player2_id) and Table B has a Primary key that relates to the foreign keys in Table A. I am trying to write a select query that will output the names of player1 and player 2 in one row.

wp_divisions - Div_id, div_player1_id, div_player2_id
wp_players - ID, display_name

Output = Div_Id, display_name of Player1, display_name of Player2

Select display_name FROM wp_players INNER Join wp_divisions where wp_players.ID = wp_divisions.div_player1_id;

This shows the Player1 names.

Select display_name FROM wp_players INNER Join wp_divisions where wp_players.ID = wp_divisions.div_player2_id;

This shows Player2 name.

I would like to have the output in a Table with:

Division    Player 1     Player 2
  1001     Joe Smith     Tom Jones

I tried following Table with two foreign keys pointing to the same column of another table

But could not adapt it to my needs

Not sure how to write the code.

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
ItsEricJS
  • 19
  • 5
  • (Obviously--) This is a faq. Before considering posting please always google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names; read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. PS Explain about how you "could not adapt it to my needs". And what about the many, many other duplicates? – philipxy Jul 01 '19 at 01:09
  • Please in code questions give a [mre]--cut & paste & runnable code; example input with desired & actual output (including verbatim error messages); tags & clear specification & explanation. That includes the least code you can give that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) – philipxy Jul 01 '19 at 01:09
  • Possible duplicate of [How to get matching data from another SQL table for two different columns: Inner Join and/or Union?](https://stackoverflow.com/questions/27682228/how-to-get-matching-data-from-another-sql-table-for-two-different-columns-inner) – philipxy Jul 01 '19 at 02:01

1 Answers1

0

You must join wp_divisions with wp_players twice:

select
  d.Div_id,
  p1.display_name player1,
  p2.display_name player2
from wp_divisions d
inner join wp_players p1 on p1.ID = d.div_player1_id
inner join wp_players p2 on p2.ID = d.div_player2_id

If there is a case that div_player1_id or div_player2_id is null then use left joins instead of inner joins.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • Worked perfectly. to add onto this, if d.div_player2_id = 0, then Set d.div_player2_id = 125; select d.player_div_id AS Division, p1.display_name player1, p2.display_name player2 from wp_divisions d inner join wp_players p1 on p1.ID = d.div_player1_id inner join wp_players p2 on p2.ID = d.div_player2_id WHERE IF (d.div_player2_id IS 0, d.div_player2_id IS 125); Fails – ItsEricJS Jun 30 '19 at 15:56
  • What is `WHERE IF (d.div_player2_id IS 0, d.div_player2_id IS 125)`? Use `WHERE d.div_player2_id = 0 AND d.div_player2_id = 125)` – forpas Jun 30 '19 at 15:59
  • It was meant to replace a 0 with 125 – ItsEricJS Jun 30 '19 at 16:18
  • In Table wp_divisions are Player 1 and Player 2. For Singles Player 2 has an ID of 0. For Doubles there is a Player 2 ID other than 0. The ealier slect string works perfect for when it is Doubles, but when it is Singles no information is found. I created a Player id 125 that has a display_name = blank. – ItsEricJS Jun 30 '19 at 16:32
  • 1
    This Singles and Doubles is something that you may understand, but I don't. I can't see how a table design where the same player has 2 different ids would work. This answer has the code relating to your question as you posted it. If the requirement is different post a new question and explain what you want. – forpas Jun 30 '19 at 16:38
  • It was related to Tennis. Singles and Doubles players and each player has their own player id. Thank you very much. You answered my question. – ItsEricJS Jul 13 '19 at 20:07