David
David

Reputation: 237

How to join three different tables in mysql with no common field amongst them all

If I have table 1 with fields Groupid and Branchid amonst others, table 2 with fields Group id and Groupname amongst others and table 3 with fields Branchid and Branchname amongst others, how do I join these tables?

natural join does not work.

Upvotes: 0

Views: 658

Answers (2)

Narnian
Narnian

Reputation: 3908

This is what the query should likely be typically. Is this different that what you have?

(SQL Server syntax)

SELECT Column1, Column2...
FROM GroupBranch_Rel
  INNER JOIN Groups
    ON Groups.GroupID = GroupBranch_Rel.GroupID
  INNER JOIN Branches
    ON Branches.BranchID = GroupBranch_Rel.BranchID

Upvotes: 0

Michael Petrotta
Michael Petrotta

Reputation: 60902

SELECT foo
FROM Table1
    JOIN Table2 ON Table2.GroupID = Table1.GroupID
    JOIN Table3 ON Table3.BranchID = Table1.BranchID

Upvotes: 1

Related Questions