Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 Select statement by joining 3 tables *new*

Author  Topic 

Ravius
Starting Member

13 Posts

Posted - 2009-11-22 : 21:50:15
Hi guys, im still learning this. hope you guys can help me out (:

I have 2 tables, "property" and "favourites".

the "property table" includes propertyid and the details
whereas the "favourites table" include buyerID and propertyid

I need to select all the details from the "property table" where the propertyid matches the propertyid in the "favourites table" that matches a particular buyerid

let say in the "favourites table", buyerid 5 is linked with propertyid 2,3,5,7. I will need to display details of property with id 2,3,5,7

Sorry if i made it very complication >.<
Thanks all of you in advance =D

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-22 : 21:57:52
[code]
SELECT p.*
FROM [property] p
JOIN [favourites] f
On f.propertyid = p.propertid
WHERE f.buyerid = xxx
[/code]
Go to Top of Page

Ravius
Starting Member

13 Posts

Posted - 2009-11-22 : 22:07:48
hey russell thank you so much!! =D really appreciate that
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-22 : 22:19:41
You're welcome. Glad to help!
Go to Top of Page

Ravius
Starting Member

13 Posts

Posted - 2009-11-23 : 03:01:28
Hey im sorry to bring this post up again... there are some changes made to the query.. sorry about that =X

i have another table call PropertyRemarks with BuyerID, PropertyID and comments...

i need to link with the statement Russell had helped me earlier on to display the comments as well (:

May i know how i can do that?? sorry for the trouble!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-23 : 03:58:30
[code]SELECT p.*,r.comments
FROM [property] p
JOIN [favourites] f
On f.propertyid = p.propertid
JOIN PropertyRemarks as r on p.propertid=r.propertid
WHERE f.buyerid = xxx
[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Ravius
Starting Member

13 Posts

Posted - 2009-11-23 : 23:20:01
sorry madhivanan, its doesn't seems to display what i need.

#################
Table

Property
-PropertyID
-*

Favourites
-BuyerID
-PropertyID

Comments
-BuyerID
-PropertyID
-remarks

#################

I have 3 tables and their properties are stated above.
Firstly buyer set a property to Favourites and then add comment to it
I need to display the details of the Favourited properties and its comment in a table.

Both the Favourites and Comments table need to take in a BuyerID.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-25 : 00:57:24
quote:
Originally posted by Ravius

sorry madhivanan, its doesn't seems to display what i need.

#################
Table

Property
-PropertyID
-*

Favourites
-BuyerID
-PropertyID

Comments
-BuyerID
-PropertyID
-remarks

#################

I have 3 tables and their properties are stated above.
Firstly buyer set a property to Favourites and then add comment to it
I need to display the details of the Favourited properties and its comment in a table.

Both the Favourites and Comments table need to take in a BuyerID.



Then alter the statement accordingly

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

wayne.zjw
Starting Member

10 Posts

Posted - 2009-12-23 : 15:58:00
select * from property table p1,favorites table p2 where p1.propertyid=p2.propertyid
maybe it can help you
good luck

Go to Top of Page
   

- Advertisement -