CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: SQL Joining

  1. #1
    Join Date
    Apr 2000
    Location
    Germany
    Posts
    122

    SQL Joining

    My SQL is getting rusty

    I have table tblA with numbers in it :

    tblA.f1
    -------
    1
    2
    3

    And tblB also with numbers in it :

    tblB.f2
    -------
    11
    22

    3 entries + 2 entries = 6 entries when I "SELECT tblA.f1, tblB.f2 from tblA, tblB;"

    1 11
    1 22
    2 11
    2 22
    3 11
    3 22

    now !!! in a table I have the entries I "Don't Want To See"

    tblC
    f1 f2
    --------
    1 11
    3 22


    How can I SQL these three tables into showing a list of 4 of the 6 entries :

    1 22 --- Note that {1 11} and {3 22} are not present because they are defined in tblC
    2 11
    2 22
    3 11

    I suspect something really strange is necessary, such as a JOIN.

  2. #2
    Join Date
    Jun 2001
    Location
    Mi
    Posts
    1,249
    Check out the LEFT OUTER JOIN ...

  3. #3
    Join Date
    Apr 2000
    Location
    Germany
    Posts
    122

    Huh?

    can you be a bit more explicit?

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    How about this one

    SELECT tblA.f1, tblB.f2 FROM tblA CROSS JOIN tblB
    WHERE NOT EXISTS (SELECT f1 FROM tblC WHERE f1 = tblA.f1 AND f2 = tblB.f2)

    The cross join appears to be the same with SELECT * FROM tblA, tblB..
    Busy

  5. #5
    Join Date
    Jun 2001
    Location
    Mi
    Posts
    1,249
    Chay,

    Nifty one that CROSS JOIN ... There is a caveat with it tho ... It is supported as far as I can tell on SQL2 ... We don't know what DB or version this person is using ... Access has about the same with a UNION query ...

    - Mike

  6. #6
    Join Date
    Apr 2000
    Location
    Germany
    Posts
    122

    Bus Driver solution

    A bus driver showed me this :

    Select tblA.f1, tblB.f2 from tblA, tblB; => put it into a temporary table, a view.
    call the the view or table 'TT'

    now
    SELECT TT.f1, TT.f2, tblC.f1, tblC f2
    FROM TT left outer join tblC
    ON tlbC.f1 = TT.f1 and
    tblC.f2 = TT.f2

    this renders

    Code:
    TT.f1   TT.f2   tblC.f1   tblC.f2
    -----------------------------------
       1      11        1       11
       1      22       null    null 
       2      11       null    null
       2      22       null    null
       3      11       null    null
       3      22        3       22
    the nulls pop up because the second table tblC is smaller than the first TT

    but those are the records we are looking for so

    SELECT TT.f1, TT.f2, tblC.f1, tblC f2
    FROM TT left outer join tblC
    ON tlbC.f1 = TT.f1 and
    tblC.f2 = TT.f2
    where tblC.f1 is NULL

    Code:
    TT.f1   TT.f2   tblC.f1    tblC.f2
    -----------------------------------
       1      22       null    null 
       2      11       null    null
       2      22       null    null
       3      11       null    null
    clean it up and :
    SELECT TT.f1, TT.f2
    FROM TT left outer join tblC
    ON tlbC.f1 = TT.f1 and
    tblC.f2 = TT.f2
    where tblC.f1 is NULL

    Code:
    TT.f1   TT.f2   
    ----------------
       1      22   
       2      11   
       2      22    
       3      11
    Solution found
    Klatau Broada Nektau

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured