CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2001
    Posts
    34

    SQL - Inner Join

    i had 3 tables that are linked together by several primary keys.
    -table1 has A and B as primary key
    -table2 has A, B, C and D as primary key
    -table3 has A, B and C as primary key
    -table1 is linked to table2 by A and B
    -table2 is linked to table3 by A, B, C and D
    my SQL inner join statement is :

    "SELECT * from " & _
    "(table1 INNER JOIN " & _
    "(table2 INNER JOIN table3 " & _
    "ON table2.A=table3.A " & _
    "and table2.B=table3.B " & _
    "and table2.C=table3.C " & _
    "and table2.D=table3.D) " & _
    "ON table1.A=table2.A " & _
    "and table1.B=table2.B)"

    Is this SQL statement true?

    Prompt reply is much appreciated.
    Thank You.



  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: SQL - Inner Join

    I'm not sure about it, I always use this syntax, also, you need to specify of what table you want to select everything.

    SELECT Table1.*
    FROM (
    Tabel1 INNER JOIN Tabel2 on (
    Tabel1.b = Tabel2.b
    ) AND (
    Tabel1.a = Tabel2.a
    )
    ) INNER JOIN Tabel3 on (
    Tabel2.d = Tabel3.d
    ) AND (
    Tabel2.c = Tabel3.c
    ) AND (
    Tabel2.b = Tabel3.b
    ) AND (
    Tabel2.a = Tabel3.a
    )




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jun 2001
    Posts
    34

    Re: SQL - Inner Join

    Thanks Cakkie, i had tried your syntax and it's the same as mine. By the way, what do you mean by the table which i want to select everything? Can you please explain.
    Thank you.


  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: SQL - Inner Join

    You say 'select * from (multiple joined tables)'. In your case, I think you will get everything of the first table mantioned in the select statement. My remark was pointed to the *, you should also include a table name there, the make sure not to get unwanted results. You really must do this when you have tables containing the same column names.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  5. #5
    Join Date
    Jun 2001
    Posts
    34

    Re: SQL - Inner Join

    i think i understand what you mean but i NOT only want data from the first table mention in the Select statement but also data from other table as well. so i dun need to include the table name before the *.is it true?
    Thank you


  6. #6
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: SQL - Inner Join

    For joined table, you can't use * to select All fields on All tables, since some of them are joined. The only way I can think of for now is choose one table as a primary table, and use * on it, but for other tables, type the exact field names one by one. Make sure not to include the same fields as in the first primary table. For example:

    select Table2.*, Table1.otherfield1, table1.otherfield2, ..., table3.otherfieldn
    from ....



    Regards,

    Michi

  7. #7
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    78

    Re: SQL - Inner Join

    You say that Table2 has ABCD as Primary Keys and that Table3 has ABC as Primary Keys, and then you say that Table2 is linked to Table3 by ABCD.... but Table3 doesn't have D as a Primary Key.

    Assuming that Table2 is linked to Table3 by ABC then:

    SELECT Table1.*, Table2.*, Table3.*
    FROM (Table1 INNER JOIN Table2 ON (Table1.B = Table2.B) AND (Table1.A = Table2.A)) INNER JOIN Table3 ON (Table2.C = Table3.C) AND (Table2.B = Table3.B) AND (Table2.A = Table3.A);

    Now, the above SQL I got from ACCESS. Usually, if the SQL is going to be complex, you should try making the tables in an ACCESS database (if you are not using it) and set the primary keys and links. I find these useful when doing LEFT and RIGHT JOINS.

    Hope it helps,

    RF


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