Click to See Complete Forum and Search --> : join statement


cs3mw
04-17-2007, 07:03 AM
Im really sorry about this because Im sure its really easy. I have the following table called soldproperty

propertyID primary key
clientID
propertytype foreign key
housenumber
sold_date

then i have a table callled property with amongst other info the following

propertyID
mainimage
description

where propertytype in the table soldproperty references the propertyID in the property table. Now what I want is to allow the client to view all the property s/he has bought which will be illustrated by the mainimage and description. I have the code to do this into a simple array but its just extracting the values from the mysql database!! anyone have any ideas?

jasonahoule
04-17-2007, 07:45 AM
Give this a try. I am not sure what server side language you are using. Just replace $userID with whatever the equal would be in your language.

SELECT *
FROM property
WHERE propertyid IN (
SELECT propertytype
FROM soldproperty
WHERE clientID = $clientID );

cs3mw
04-17-2007, 08:05 AM
yeah that seems to do the trick bar one small thing!! a client can buy a number of the same property hence the housenumber in the soldproperty table. However at the moment this is only showing the one tuple. I hope this makes sence!!

jasonahoule
04-17-2007, 08:24 AM
Ok, how about this then?

SELECT *
FROM property p, soldproperty s
WHERE p.propertyID IN (
SELECT propertytype
FROM soldproperty
WHERE clientID = $clientID )
AND p.propertyID = s.propertyID;

cs3mw
04-17-2007, 08:40 AM
yeah that works a treat the only difference was the last line instead of p.propertyID = s.propertyID it should be p.propertyID = s.propertytype. Thanks very much for your help

jasonahoule
04-17-2007, 08:43 AM
No problem. Glad to help.