1

I am trying to select data from 2 tables, Game and Team. I have a list of games in the Game table, where there are columns named htID, atID, apiID and a few others; and I would like to grab the names of these teams from the Team database.

I am currently using multiple SQL statements, which work in theory but not in my program due to the fact that I cannot open more than one database reader at a time.

SELECT * FROM Game WHERE Played = 0 ORDER BY DT ASC
SELECT TN FROM Team WHERE apiID = htID 
SELECT TN FROM Team WHERE apiID = atID 

(where htID and atID are variables in my program containing the ID's grabbed from the first SQL result)

Is there any way I can do the above with only one statement?

  • Yep, you are looking for `INNER JOIN` - https://www.w3resource.com/sql/joins/perform-an-inner-join.php – Alex K. Feb 18 '18 at 12:21
  • 1
    Possible duplicate of [How can an SQL query return data from multiple tables](https://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) – chb Feb 18 '18 at 12:34

1 Answers1

2

You need two joins. In MS Access tis requires over-user of parentheses:

select g.*, th.tn as htn, ta.tn as atn
from (game as g inner join
      team as th
      on g.htID = th.apiID
     ) innerjoin
     team as ta
     on g.atID = ta.apiID
where g.played = 0
order by date desc;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786