A Join Question
| From: | Steve Fitzgerald | Date: | Mon, 30 Jul 2001 14:15:23 +0000 |
| Subject: | A Join Question | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-10900@lists.php.net to get a copy of this message | ||
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