简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM