joining question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sugapablo

    #1

    joining question

    OK, I have two tables:

    table1:
    id, group1, group2
    -------------------
    1, 1, 3
    2, 1, 2
    3, 2, 3
    4, 1, 3


    table2:
    id, groupname
    -------------
    1, user_group
    2, old_group
    3, new_group


    In table1, group1 and group2 correspond to id in table2.

    I want to select * from table 1, but join the names from table2 so the
    results, would look like:

    1, user_group, new_group
    2, user_group, old_group
    3, old_group, new_group
    4, user_group, new_group

    Now, I know how to join one column to one column, but how do you join 2
    columns to one column (i.e. join both table1.group1 and table1.group2 to
    table2.groupnam e) in one statement?


    --
    [ Sugapablo ]
    [ http://www.sugapablo.com <--music ]
    [ http://www.sugapablo.net <--personal ]
    [ sugapablo@12jab ber.com <--jabber IM ]

  • BDR

    #2
    Re: joining question

    I think this might work:

    select A.id, B.groupname, C.groupname from table1 as A
    left join table2 as B on A.group1=B.id
    left join table2 as C on A.group2=C.id
    order by A.id
    -------------------------------------------------------
    Sugapablo wrote:[color=blue]
    > OK, I have two tables:
    >
    > table1:
    > id, group1, group2
    > -------------------
    > 1, 1, 3
    > 2, 1, 2
    > 3, 2, 3
    > 4, 1, 3
    >
    >
    > table2:
    > id, groupname
    > -------------
    > 1, user_group
    > 2, old_group
    > 3, new_group
    >
    >
    > In table1, group1 and group2 correspond to id in table2.
    >
    > I want to select * from table 1, but join the names from table2 so the
    > results, would look like:
    >
    > 1, user_group, new_group
    > 2, user_group, old_group
    > 3, old_group, new_group
    > 4, user_group, new_group
    >
    > Now, I know how to join one column to one column, but how do you join 2
    > columns to one column (i.e. join both table1.group1 and table1.group2 to
    > table2.groupnam e) in one statement?
    >
    >[/color]

    Comment

    • BDR

      #3
      Re: joining question

      I think this might work:

      select A.id, B.groupname, C.groupname from table1 as A
      left join table2 as B on A.group1=B.id
      left join table2 as C on A.group2=C.id
      order by A.id
      -------------------------------------------------------
      Sugapablo wrote:[color=blue]
      > OK, I have two tables:
      >
      > table1:
      > id, group1, group2
      > -------------------
      > 1, 1, 3
      > 2, 1, 2
      > 3, 2, 3
      > 4, 1, 3
      >
      >
      > table2:
      > id, groupname
      > -------------
      > 1, user_group
      > 2, old_group
      > 3, new_group
      >
      >
      > In table1, group1 and group2 correspond to id in table2.
      >
      > I want to select * from table 1, but join the names from table2 so the
      > results, would look like:
      >
      > 1, user_group, new_group
      > 2, user_group, old_group
      > 3, old_group, new_group
      > 4, user_group, new_group
      >
      > Now, I know how to join one column to one column, but how do you join 2
      > columns to one column (i.e. join both table1.group1 and table1.group2 to
      > table2.groupnam e) in one statement?
      >
      >[/color]

      Comment

      • Sugapablo

        #4
        Re: joining question

        BDR wrote:
        [color=blue]
        > select A.id, B.groupname, C.groupname from table1 as A
        > left join table2 as B on A.group1=B.id
        > left join table2 as C on A.group2=C.id
        > order by A.id[/color]

        Thank you, that worked well.

        --
        [ Sugapablo ]
        [ http://www.sugapablo.com <--music ]
        [ http://www.sugapablo.net <--personal ]
        [ sugapablo@12jab ber.com <--jabber IM ]

        Comment

        • Sugapablo

          #5
          Re: joining question

          BDR wrote:
          [color=blue]
          > select A.id, B.groupname, C.groupname from table1 as A
          > left join table2 as B on A.group1=B.id
          > left join table2 as C on A.group2=C.id
          > order by A.id[/color]

          Thank you, that worked well.

          --
          [ Sugapablo ]
          [ http://www.sugapablo.com <--music ]
          [ http://www.sugapablo.net <--personal ]
          [ sugapablo@12jab ber.com <--jabber IM ]

          Comment

          Working...