|
-
Jun 21st, 2006, 08:57 AM
#1
Thread Starter
Addicted Member
[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.
-
Jun 21st, 2006, 09:17 AM
#2
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
-
Jun 21st, 2006, 09:23 AM
#3
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
-
Jun 21st, 2006, 09:43 AM
#4
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.
-
Jun 21st, 2006, 09:52 AM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|