[PHP-DB] join query/relational database question

2003-01-06 Thread Doug Parker
I have a relational database issue that I'll simplify for the sake of 
this post.  I have one table, called clients,  that has an id and the 
company name.  For example:

id 
| 
company
---
46 
	Jones Inc.
54 
	Baker Inc.	

etc.

I have another table called projects, that looks like this:

id 
| 
company_id 
| 
status
---
1 
	46			Active
2 
	54			Inactive

etc.

In this case, it is relational in that company_id is the key to the 
clients table, for naming purposes.  My question is, is it possible to 
do a query across two tables that could retrieve all of the information 
in the projects table AND have the 'company' field in the clients table 
come up as well.  So it would be like this:

id 
| 
company_id 
| 
status 
	|	company

1 
	46			Active			Jones Inc.
2 
	54			Inactive		Baker Inc. 
		




any help would be greatly appreciated...


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] join query/relational database question

2003-01-06 Thread Paolo Bonavoglia
At 16:09 06/01/2003 06/01/2003, Doug Parker wrote:

I have a relational database issue that I'll simplify for the sake of this 
post.  [...] So it would be like this:

id | company_id | status|   company

1   46  Active  Jones Inc.
2   54  InactiveBaker 
Inc.


Using SQL the following SELECT should be the solution:

SELECT projects.id, company_id, status, company
FROM projects, clients
WHERE projects.company_id = clients.id

You can of course add any other filter in the WHERE clause using the usual 
AND OR NOT operators.

Of course you have to embed this command in an appropriate PhP statement, 
e.g. using the odbc library, and having an open connection $conn to the DB:

odbc_exec($conn, 'SELECT ... ');

Hope this will be enough to solve your problem.



Paolo Bonavoglia

Cannaregio 3027/R
30121 V E N E Z I A

Sito del Liceo Foscarinihttp://www.liceofoscarini.it/
Astronomia e 
Calendari  http://digilander.iol.it/paolobona/astro/home.html


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] join query/relational database question

2003-01-06 Thread Rick Widmer
At 04:09 PM 1/6/03 -0500, Doug Parker wrote:

I have a relational database issue that I'll simplify for the sake of this 
post.  I have one table, called clients,  that has an id and the company 
name.  For example:

id | company
---
46  Jones Inc.
54  Baker Inc.

etc.

I have another table called projects, that looks like this:

id | company_id | status
---
1   46  Active
2   54  Inactive


id | company_id | status|   company

1   46  Active  Jones Inc.
2   54  InactiveBaker 
Inc.

SELECT id, company.company_id, status, company
FROM projects
LEFT JOIN Companies USING( company_id )
ORDER BY company

Rick


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php