conditional join

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

    #1

    conditional join

    I have made a basic left join statement say "Select t.*, p.usenet_id
    from usenet_group_1 u left join user_table t on u.id = t.usenet_id;
    but only want the recordset to return values from user_table if field
    in user_table have a certain value ( if usenet_group = 5 ) - can this
    be done within the above sql statement or do i have to make 2 seperate
    statements?

    regards
    Jakob
    outzen@HaHapost 6.tele.dk
    Cut out the laughing if replying to email address
  • Jakob Outzen

    #2
    Re: conditional join

    Well - found the answer myself - it was really to easy: Just need to
    add the field, ie;
    "Select t.*, p.usenet_id, p.usenet_group from usenet_group_1 u left
    join user_table t on u.id = t.usenet_id and p.usenet_group = 5;

    :- |

    On Thu, 29 Dec 2005 07:17:08 GMT, outzen@HaHapost 6.tele.dk (Jakob
    Outzen) wrote:
    [color=blue]
    >I have made a basic left join statement say "Select t.*, p.usenet_id
    >from usenet_group_1 u left join user_table t on u.id = t.usenet_id;
    >but only want the recordset to return values from user_table if field
    >in user_table have a certain value ( if usenet_group = 5 ) - can this
    >be done within the above sql statement or do i have to make 2 seperate
    >statements?
    >
    >regards
    >Jakob
    >outzen@HaHapos t6.tele.dk
    >Cut out the laughing if replying to email address[/color]

    outzen@HaHapost 6.tele.dk
    Cut out the laughing if replying to email address

    Comment

    • Bill Karwin

      #3
      Re: conditional join

      "Jakob Outzen" <outzen@HaHapos t6.tele.dk> wrote in message
      news:43b389e2.3 50813937@news.i net.tele.dk...[color=blue]
      >I have made a basic left join statement say "Select t.*, p.usenet_id
      > from usenet_group_1 u left join user_table t on u.id = t.usenet_id;
      > but only want the recordset to return values from user_table if field
      > in user_table have a certain value ( if usenet_group = 5 )[/color]

      Yep, you can specify conditions in the ON clause of an outer join.
      Rows that don't match are not included in the join.
      For example:

      SELECT t.*, p.usenet_id
      FROM usenet_group_1 u LEFT OUTER JOIN user_table t
      ON (u.id = t.usenet_id AND t.usenet_group = 5)

      Regards,
      Bill K.


      Comment

      Working...