Results 1 to 5 of 5

Thread: [Resolved]Is the DataBase Design correct ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Location
    United
    Posts
    202

    Resolved [Resolved]Is the DataBase Design correct ?

    Hi i have a problem in writing a query , here goes the problem .
    I have a table structure like this

    [Loan TABLE]
    loanId
    loandate
    returnId
    returndate

    [User TABLE]
    ID
    NAME

    1)loan.loadid has a relationship with user.id
    2)loan.returnid also has a relationship with user.id

    Well because the loanid and returnid has a relationship with user.id , is it possible to create a join to display loanid and returnid with respect to name ?
    e.g.
    [loan table]
    loanid loandate returnid returndate
    1 monday 3 monday
    2 tuesday 1 monday
    [user table]
    id name
    1 alan
    2 charlie
    3 daniel

    Below is the result i which to get
    [result]
    alan monday daniel monday
    charlie tuesday alan monday

    [What i have tried]
    select user.name , loandate , user.name,returndate from loan inner join user on loan.loanid=user.id and loan.returnid=user.id
    Last edited by GOBI; Jun 21st, 2006 at 09:53 AM.

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

    Re: Is the DataBase Design correct ?

    Try this:

    Code:
    Select * From [Loan Table] LT
     Left Join [User Table] U1 on U1.Id=LT.LoanId
     Left Join [User Table] U2 on U2.Id=LT.ReturnId

    *** 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

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Is the DataBase Design correct ?

    Yes, you simply join to two different versions of the user table.

    Code:
    Select Loaner.Name, Loan.LoanDate, Returner.Name, Loan.ReturnDate
    From Loan
    Inner join user as Loaner
       on Loan.LoanID = User.ID
    Inner join user as Returner
       on Loan.ReturnID = User.ID
    You probably don't want a table name of user though as it's almost certainly a reserved word

    edit - damn, beaten to the punch

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

    Re: Is the DataBase Design correct ?

    I agree!

    even worse to have those spaces in the table names - although the []'s will work...

    I've got a link in my sig to a list of reserved words that should be avoided.

    btw - INNER JOIN will eliminate the LOAN record is the USERID do not join to a valid user row - just wanted to clarify that.

    *** 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
    Addicted Member
    Join Date
    Feb 2002
    Location
    United
    Posts
    202

    Resolved Re: Is the DataBase Design correct ?

    Thanks and appreciate the help from both of you

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