Skrevet av Emne: SQL: Joining the same table twice (or more)  (Lest 4363 ganger)

ATC

  • Gjest
SQL: Joining the same table twice (or more)
« på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • In table A, "A.foo" and "A.bar" both refer to records in table B. How to join both in a single SQL statement?



    ATC

    • Gjest
    [Solved] SQL: Joining the same table twice (or more)
    « Svar #1 på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • 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)