SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Combining Left and Inner Joins

    Hello,

    Is it possible to include an inner join within a series of left joins?

    In other words:

    Code MySQL:
    Select TableA.*
    FROM TableA
    LEFT JOIN TableB
    ON TableA.x=TableB.x
    LEFT JOIN ????
    (TableC INNER JOIN TableD
    WHERE TableC.y > TableDy)

    I put the question marks to indicate that I THINK I need some sort of alias for the subsequent inner join (if this is even possible...).

    Thanks!

    -Eric

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    The inner joins would need to come before the left joins.
    The only code I hate more than my own is everyone else's.

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    It depends on what you need.
    Code:
    SELECT TableA.*
    FROM TableA
    INNER JOIN TableD
    ON ....
    LEFT JOIN TableB
    ON TableA.x=TableB.x
    LEFT JOIN TableC 
    ON ...
    Code:
    SELECT TableA.*
    FROM TableA
    LEFT JOIN TableB
    ON TableA.x=TableB.x
    LEFT JOIN 
      (SELECT
          ....
       FROM TableC 
       INNER JOIN TableD
       ON TableC.y > TableDy
      ) AS alias
    ON ....
    are two different queries that will give different results.

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you both. The syntax makes sense and I can see the differences in guido's response.

    -Eric


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •