What type of join is this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AllBeagle
    New Member
    • Sep 2008
    • 13

    What type of join is this?

    Hey all,

    I need to join 2 tables like the following scenario below, can anyone enlighten me as to what type of join this would be (or better yet an example of how the query would be set up)?
    .....

    Example -

    Student Table A has 5 columns (firstname, lastname, gender, section_A_id, section_B_id)

    Student Table B has 2 columns (id, sectionname)

    I want this returned in 1 record...

    firstname, lastname, gender, section_A_id, section_B_id, sectionname(for section_A_id), sectionname (for section_B_id)
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Looks like you need to JOIN to Student Table B twice, something like
    Code:
    SELECT firstname, lastname, gender, section_A_id, section_B_id, BA.sectionname, BB.sectionname 
    FROM A 
    JOIN B AS BA ON id = section_A_id
    JOIN B AS BB ON id = section_B_id

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      @AllBeagle,

      If you are guaranteed that table A will always refer to two rows in table B, code green gave you a good answer.

      Otherwise, you'll need to use outer joins, something like this, perhaps....

      Code:
      SELECT firstname, lastname, gender, section_A_id, section_B_id, BA.sectionname, BB.sectionname  
      FROM A  
         LEFT OUTER JOIN B AS BA ON BA.id = A.section_A_id 
         LEFT OUTER JOIN B AS BB ON BB.id = A.section_B_id

      Comment

      Working...