Results 1 to 5 of 5

Thread: Join 2 tables

  1. #1
    Join Date
    Dec 2003
    Posts
    13

    Unanswered: Join 2 tables

    Maybe this is trival, but I am more an application coder than database engineer:

    I have two tables from which I would like to get my data.

    Table1 (main table, has more entries than Table2) :
    ID, foo2, foo3, foo4

    Table2:
    ID, bar2, bar3, bar4

    Now what I would like to get is something like this:

    foo2,foo3,bar2,bar3 ( if (table1.ID = 'dummy') AND (table2.ID = 'dummy') AND (foo2 = bar2) )

    foo2,foo3,NULL,NULL ( if (table1.ID = 'dummy') and there is no matching bar2 for foo2 )

    I guess this could be a JOIN but I don't have clue.


    Thanks,
    kromo

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    that's a left outer join:
    Code:
    select foo2, foo3, bar2, bar3
      from table1
    left outer
      join table2
        on table1.ID = table2.ID 
     where table1.ID = 'dummy'
    rudy
    http://r937.com/

  3. #3
    Join Date
    Dec 2003
    Posts
    13
    First, thanks.

    Second:
    "ID" is not unique, only the combination of "ID" and "foo2" is unique.
    Does this statement work for this? Or do I have to write something like this:

    Code:
    select t1.foo2, t1.foo3, t.2bar2, t2.bar3
      from table1 t1
    left outer
      join table2 t2
        on (t1.ID = t2.ID) AND (t1.foo2 = t2.bar2)
     where table1.ID = 'dummy'
    kromo

  4. #4
    Join Date
    Dec 2003
    Posts
    13
    Thanks a lot, it worked.
    kromo

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    oops, yeah, i missed the second join condition, but you've obviously gotten the idea


Posting Permissions

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