Ano-Tech Computers
Enter keyword:

SQL: Joining the same table twice (or more)
Problem:
In table A, "A.foo" and "A.bar" both refer to records in table B. How to join both in a single SQL statement?
 
Solution:
Use JOIN..AS like this:

SELECT A.*, fooB.*, barB.*
FROM A
LEFT JOIN B AS fooB ON (A.foo = fooB.id)
LEFT JOIN B AS barB ON (A.bar = barB.id)

 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles