Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-25-2006, 06:34 AM   #1 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
JOIN Syntax Help

Hello,

My Situation:
I want to use the query results of my code below to query another table in the same database. Then I want to display the results of that(the second) query. How do I do this? I believe I'm supposed to use some sort of JOIN statement. I've read the MySql manuals but I don't understand what they are talking about.

Additional Information:
The other table is called developer & there are 3 fields in it. The field names are NAME, PROJECT & TYPE. So the results of field company should query NAME. The results of field project should query PROJECT. The results of field property should query TYPE.

The 1st part of my code is below. I don't know how to write the 2nd part. I appreciate your help on this matter.
PHP Code:
<?php 
$username
="abc23"
$password="abc23"
$database="abc23"
$host="localhost"

mysql_connect ("$host","$username","$password"); 
mysql_select_db($database) or die( "Where's the database man?"); 

$mktime date('Y-m-d'); 

$query1=("SELECT * FROM developer_log WHERE expiry > '$mktime'"); 
     
$result1=mysql_query($query1); 
$num=mysql_num_rows($result1); 

while (
$row1 mysql_fetch_array($result1)) 

echo
"<b>Main Location: </b> ".$row1['id']. 
"<p><b>Sub Location: </b> ".$row1['company']. 
"<p><b>Percentage of Malays: </b> ".$row1['project']. 
"<p><b>Percentage of Chinese: </b> ".$row1['property']. 
"<p><b>Percentage of Indians: </b> ".$row1['email']. 
"<p><b>Percentage of Others: </b> ".$row1['cc_email']. 
"<p>" 


?>
transfield is offline   Reply With Quote
Old 05-25-2006, 08:31 AM   #2 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
You're right, you want to JOIN your tables together into a virtual table to get all your data in one query. Here's how I would do it:

Code:
$query = "
SELECT developer_log.*, developer.* 
FROM developer_log LEFT JOIN developer 
USING (project)";
// Add WHERE developer.name = "name" at the end to restrict results 
// to only one particular developer. A good idea would be to add a 
// "developer_id" field to both the developer_log, and developer tables.
What this does is "join" the tables together into a virtual table which only lasts the duration of the the query. the USING (project) bit simply says that when the tables are joined together, use the "project" key to match rows from one table to the rows from the other. "WHERE developer_log.project = developer.project" would have the exact same effect, but use the "USING" command whenever the field names are the same. This helps keep your WHERE conditional statements a little less cluttered when you start making more complicated queries.
__________________
Current Project
Redline is offline   Reply With Quote
Old 05-25-2006, 08:38 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
i know you tried to explain it clearly, but i don't completely get it yet.

table1: developer_log
id
company
project
property
email
cc_email

table2: developer
name
project
type

ok, so the 'project' field is the relation between these 2 tables right?

if you wanted all the developer information in each row of the original query, and they were joined on the project, your query might look like this. ( although i'm not sure if this is what you are going for )

Code:
select * from developer_log as dl
  left join developer as d on dl.project=d.project
  where dl.expiry > '$mktime'
__________________
Mike
sde is offline   Reply With Quote
Old 05-25-2006, 08:41 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
looks like redline beat me to it while i had the page open for a while .. both queries should do the same thing.
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Left outer join problem snauw Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 02-01-2005 10:39 PM
Syntax Highlighter Rafkin PHP 6 12-06-2004 05:19 PM
PHP Syntax Query (What is this ?) JeC PHP 2 06-23-2004 03:49 AM
highlight_string($string) - php syntax highlighting sde PHP 0 07-30-2002 02:19 PM


All times are GMT -8. The time now is 10:42 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting