Results 1 to 5 of 5

Thread: Joins with three tables[Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    53

    Joins with three tables[Resolved]

    Hi,
    I want this result:

    ProductName FirstName SecondName
    Server Jesse James
    PC Peter Parker
    Laptop null null


    My problem is that ProductName comes from a different table, First Name and Last Name from user table and there is an association between the two in another table

    I have tried writing joins for this but am struggling right now. This was my earlier query when Firstyname and second name could not be null

    select product.productname, userprofile.firstname, userprofile.secondnamefrom userprofile, product, facilityuserassoc where facilityuserassoc.fk_product_user_type_id = 1 and facilityuserassoc.fk_product_id = product.pk_product_id and facilityuserassoc.fk_user_id = userprofile.pk_user_id

    Can you please have a look into this.
    Last edited by lpere68; Apr 12th, 2005 at 03:24 PM.
    I know that I know nothing.

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Joins with three tables

    Try this ..
    You'll probably have to switch LEFT for RIGHT join depending on witch table is the father VS child,. with I don't know

    Code:
     
    SELECT 
    product.productname, 
    userprofile.firstname, 
    userprofile.secondname
    FROM 
    facilityuserassoc 
    	LEFT JOIN product
    	 ON acilityuserassoc.fk_product_id = product.pk_product_id
    	LEFT JOIN userprofile
    		ON facilityuserassoc.fk_user_id = userprofile.pk_user_id

  3. #3
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Joins with three tables


  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Joins with three tables

    Do you want the product to appear even if no users are associated with it? That appears to be the case since you show LAPTOP with no one...

    Can a product have more than one associated user?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    53

    Re: Joins with three tables

    Yes,
    A product may have no users associated with it

    and

    No,
    A product can have a maximum of 1 user

    Thanks Zakary,

    It Works.

    Now, I have to insert a couple of more tables into it
    I know that I know nothing.

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