Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-05-2004, 09:34 PM question about Joins
keira's Avatar
Novice Talker

Posts: 7
Trades: 0
Hi, i don't quite understand joins ..

if i were to search for 'propSuburb' which exists in two tables (suburb, and property) do i have to do a join to show my results properly??
if so.. how do i do an inner join with this query.. ??

<php>
$query = "Select distinct propSuburb from property
where propType='$propType' ORDER BY propSuburb";
</php>
keira is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-05-2004, 11:24 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Keira, in the example you are describing, there is no need for a join; only two queries.

A join is more appropriate when you have a relation between two tables. For example, imagine I have three tables.

An Actors table with just two fields, actorId (primary key) and actorName.

A Movies table with just two fields, movieId (primary key) and movieName.

An Acted table with just three fields, id (primary key) actorId and movieId.

Now the Actors table would be filled with actors and actresses but nothing about their movie work. Movies would be filled movies, but no information on who acted in them. The Acted table bridges the two and has a relationship with both.

So let's say we had a movie, Jaws and we wanted to find out all the actors in it.

PHP Code:
SELECT m.movieNamea.actorNamec.* FROM Movies m INNER JOIN Acted c on m.movieId c.movieId INNER JOIN Actors a on a.actorId c.actorId WHERE m.movieName 'Jaws' 
Now this is a bit more complex example than I needed (sorry, thought it up on the fly) because it has two joins rather than one. The final result will be a result set with 5 fields, or columns: movieName (Jaws), actorName (containing all the actors in the movie) and the three fields from the Acted table (which aren't necessary, but I included them as an illustration of the concept).

I hope I helped rather than made it worse.....

But in your example, you have two similar tables who really don't have a relationship with each other, right? They are independent lists of properties? I'm making that assumption. If that is the case, then you can get a complete list of properties of a given property type by running two separate queries.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 08-05-2004, 11:38 PM
keira's Avatar
Novice Talker

Posts: 7
Trades: 0
okay your example did confuse me a bit with the two joins, but.. i'll understand that one day perhaps..
as far as my question goes though, my query should be fine the way it is, or do you recommend i do two queries??

i thought that mysql was just really similar to microsoft access, so i think im confusing myself a lot..

question/ say i have a table called property, and the user enters the information about a property including an address, suburb, etc..
the type of program im using to search the database, and to enter new data requires you to first search via suburb, so i thought maybe i should make a new table soley for 'suburb'.. so the user can choose from an existing suburb or enter a new one into the database when entering data for a new property. ..

er.. is this making sense?
what i want to know is, do i really need another table for suburb?? i cant get my head around it.. i really am really really new at this! lol... sorry!!!
keira is offline
Reply With Quote
View Public Profile
 
Old 08-06-2004, 02:38 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
keira,

If you are going to have thousands of records in the property table, then perhaps you would want a separate table for your suburb list. In that case, you would have a two-column table, suburbId (an int primary key) and suburb name. Then in your property table you would put the primary key of the suburb the property is in (not the name), so that column would be an int.

Then, in that case, to pull all the properties in a given suburb, you would pull it like this:


SELECT p.*, s.* FROM properties p INNER JOIN suburbs s on p.suburbId = s.suburbId where s.suburbName = 'ALEXANDRIA'


You will also be wanting to list your available suburbs and this is easily done by

SELECT * FROM suburbs
__________________
—Kyrnt

Last edited by Kyrnt; 08-07-2004 at 10:30 AM..
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 08-06-2004, 11:48 PM
keira's Avatar
Novice Talker

Posts: 7
Trades: 0
thanks Kyrnt,
that helps a lot!!! I might even be closer to understanding this after all!!
keira is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to question about Joins
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41836 seconds with 12 queries