[PHP-DB] A Join Question From: Steve Fitzgerald (sf <email protected>)
Date: 07/30/01

I'm trying to create a join statement that pulls out a CompanyName based
on a given CompanyID that is tied to a specified ContactID.

For example, if ContactID=1 then the corresponding CompanyName might be
Smith, Inc. depending on what was entered.

Here are the tables.

table = Contacts
----------------------------------------------------------------------------
FIELD | TYPE | ATTRIBUTES | NULL | DEFAULT | EXTRA |
----------------------------------------------------------------------------

ContactID | INT(4) | UNSIGNEED | NO | AUTO_INCREMENT | <---- Primary
Key/Unique
CompanyID | INT(4) |

// more fields
----------------------------------------------------------------------------

table = Company
----------------------------------------------------------------------------
FIELD | TYPE | ATTRIBUTES | NULL | DEFAULT | EXTRA |
----------------------------------------------------------------------------
CompanyID | INT(4) | UNSIGNEED | NO | AUTO_INCREMENT | <---- Primary
Key/Unique
CompanyName | INT(4) |

//more fields
----------------------------------------------------------------------------
---------------------------------------------------------------------

What I have tried so far is:

SELECT * FROM contacts,company WHERE contacts.CompanyID=$ContactID and
companyID= $ContactID

I've tried slightly other variations including:

SELECT CompanyName,WebSite
FROM $table_name1,$table_name2
WHERE contacts.CompanyID='$CompanyID'

also,

SELECT *
FROM Contacts,Company
WHERE
Contacts.CompanyID=Company.CompanyID AND
Contacts.CompanyID='$ContactID'

With this I get an empty set.

None of these seem to work. I'm obviously not doing the join right, but
I'm not sure what I'm leaving out.

Thanks.

Steve

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe <email protected>
For additional commands, e-mail: php-db-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>