0

coachID and teamCoach contain the same data but have different column names. I'm having trouble joining them as a result. What can I do?

SELECT tblCoaches.coachID AS "Coach ID", 
       tblCoaches.coachName AS "Coach Name", 
       tblCoaches.coachCity AS "Coach City", 
       tblTeams.teamName AS "Team Name"
FROM tblCoaches
INNER JOIN tblTeams ON **tblCoaches.coachID = tblTeams.teamCoach**
      AND (coachID IN (SELECT teamCoach FROM tblTeams))
WHERE teamIsRec IS NULL;
0

2 Answers 2

1

If you want to make join on specific columns:

select * from table2 JOIN table1 ON table2.id = table1.id_a

In your case, I think that the line

AND (coachID IN (SELECT teamCoach FROM tblTeams))

doesn't perform any logic because INNER JOIN takes only those column values that are both in two tables. So you syntax should looks like:

SELECT tblCoaches.coachID AS "Coach ID", tblCoaches.coachName AS "Coach Name", tblCoaches.coachCity AS "Coach City", tblTeams.teamName AS "Team Name"
FROM tblCoaches
INNER JOIN tblTeams ON tblCoaches.coachID = tblTeams.teamCoach
WHERE teamIsRec IS NULL;
Sign up to request clarification or add additional context in comments.

3 Comments

Okay, so as long as the data is the same, it doesn't matter that the names don't match. Thank you!
Good job for copying my answer
Or maybe you mine?
0

This structure should work

    SELECT
      tblCoaches.coachID AS "Coach ID",
      tblCoaches.coachName AS "Coach Name",
      tblCoaches.coachCity AS "Coach City",
      tblTeams.teamName AS "Team Name"
    FROM tblCoaches
    INNER JOIN tblTeams
      ON tblCoaches.coachID = tblTeams.teamCoach   -- IDs that linked the tables
    WHERE teamIsRec IS NULL;

2 Comments

So you removed the AND statement (which I kind of just threw in there, to be honest), but I've already tried that. It doesn't give me any results. Regardless, it's acceptable to say ON tblCoaches.coachID = tblTeams.teamCoach? Even though the column names aren't the same?
You did not post your table definition, how can I tell if you did the right join.... i.e. if TeamCoach is an ID then should be able to join it with the CoachID otherwise replace teamcoach with something else that identifies the coach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.