Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Unanswered: Help with nested left join

    I'm new here so hello everyone. []

    I have these 2 tables:
    MESSAGE_ELEMENTS:
    ELEMENT_PK
    ELEMENT_NAME
    MESSAGE_TYPE

    SCHEMA_ELEMENTS:
    SCHEMA
    ELEMENT_PK (this is FK from ELEMENTS)
    SUPPORT

    The first table stores a list of elements in message types (each message type is made of a list of elements).
    The second table stores which fields in a message type are valid in a particular schema.

    http://farm4.static.flickr.com/3112/...9c6b49.jpg?v=0

    What I want is to get a list of elements for a message type and display the support details for a particular schema:
    http://farm4.static.flickr.com/3101/...063d9b.jpg?v=0

    I tried something like this:
    SELECT MESSAGE_ELEMENTS.*
    FROM MESSAGE_ELEMENTS
    LEFT JOIN
    (SELECT SCHEMA_ELEMENTS.*, RESULT.LEVEL
    FROM SCHEMA_ELEMENTS
    WHERE SCHEMA_ELEMENTS.SCHEMA_NAME = [schema name] AS RESULT)
    ON
    MESSAGE_ELEMENTS.ELEMENT_PK = RESULT.ELEMENT_PK
    WHERE MESSAGE_ELEMENTS.MESSAGE_TYPE = [message type]

    I tried all kind of variations on this and always get errors or wrong results.
    What am I doing wrong?
    Thanks
    Last edited by marxeil; 01-02-09 at 14:37.

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Code:
    SELECT MESSAGE_ELEMENTS.* 
    FROM MESSAGE_ELEMENTS
    LEFT JOIN 
    (SELECT SCHEMA_ELEMENTS.*, RESULT.SUPPORT_LEVEL
    FROM SCHEMA_ELEMENTS
    WHERE SCHEMA_ELEMENTS.SCHEMA_NAME = [schema name]) AS RESULT
    ON 
    MESSAGE_ELEMENTS.ELEMENT_PK = RESULT.ELEMENT_PK
    WHERE MESSAGE_ELEMENTS.MESSAGE_TYPE = [message type]
    This should correct the errors at least.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you'll still get an error because RESULT.SUPPORT_LEVEL references an outer alias
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Jan 2009
    Posts
    2
    Quote Originally Posted by pootle flump
    Code:
    SELECT MESSAGE_ELEMENTS.* 
    FROM MESSAGE_ELEMENTS
    LEFT JOIN 
    (SELECT SCHEMA_ELEMENTS.*, RESULT.SUPPORT_LEVEL
    FROM SCHEMA_ELEMENTS
    WHERE SCHEMA_ELEMENTS.SCHEMA_NAME = [schema name]) AS RESULT
    ON 
    MESSAGE_ELEMENTS.ELEMENT_PK = RESULT.ELEMENT_PK
    WHERE MESSAGE_ELEMENTS.MESSAGE_TYPE = [message type]
    This should correct the errors at least.
    That's it. I posted this in 2 other forums and only you came through. Thanks a lot.

Posting Permissions

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