Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2003
    Posts
    33

    Question Unanswered: SQL query problem

    I have 4 tables (connected by foreing keys) :A,B,C,D and i have to query table D by attributes of tables A,D,C,D.

    In previous application i did the same task on tables A,B (get all B elements by attributes of A,B) I used the following query (that worked right ):
    SELECT DISTINCT B.* FROM B LEFT JOIN A ON B.key = A.foreing key WHERE ..."

    Now I try to perform the same with 4 tables:
    SELECT DISTINCT D.* FROM D LEFT JOIN C LEFT JOIN B LEFT JOIN A ON D.key=C.foreing key AND C.key = B.foreing key AND B.key = A.foreing key WHERE ..."

    But I get SQL server2000 error : wrong sysntax near WHERE. I think the problem is "D LEFT JOIN C LEFT JOIN B LEFT JOIN A "... May be someone knows what is the problem?
    Thank you
    Yulia

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    select distinct D.* 
      from D 
    left outer
      join C 
        on D.key=C.foreignkey  
    left outer
      join B 
        on C.key=B.foreignkey  
    left outer
      join A 
        on B.key = A.foreignkey 
     where ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2003
    Posts
    33

    Thumbs up

    WoW! thank you, it works! and I was shure that mission impossible
    Thank you
    Yulia

Posting Permissions

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