Forums

PHP

This topic is locked

Display result with a pivot table ?

Posted 26 Oct 2001 21:42:48
1
has voted
26 Oct 2001 21:42:48 philippe lê posted:
Hi

I have a table that contains personnal information, I have another that contains project informations.
I have another table that links one person to the different projects he works on (one person can work on multiple projects).
I use the primary keys to make the links.

So when I make a query to know on wich project a person works I get the projects primary key back (for exemple person n°1 works on projects 1,2,8,12...)

I would like to know how I can create a repeated region that list the projects of that person but instead of printing the primary key of the project prints out the name of the project, this name is stored in the project table, that means, everytime the server prints the project number he makes also a query on the project table to get its name.

Thanks

Replies

Replied 26 Oct 2001 21:52:08
26 Oct 2001 21:52:08 Bruno Mairlot replied:
You must use a JOIN statement in your SQL query.

Suppose you have the following table :

Table Personnal
---------------
ID (primary key)
name
info2
info3
.
.

Table Project
-------------
ID (primary key)
name (name of the project)
.
.

Table link_pp (link_project_personne shortcut)
-------------
personnalID (key to table personnal)
projectID (key to project)

This is the same schema you have, I suppose ?

The the query you should do is :
<pre id=code><font face=courier size=2 id=code>
SELECT personnal.name as persName,project.name as projectName FROM link_pp LEFT JOIN project ON link_pp.projectID=projectID LEFT JOIN personnal ON personnal.ID=link_pp.personnalID
</font id=code></pre id=code>

The join keyword allows you to ,he, join two tables with their keys. Note that the ON statement are where you say :"These key must be the same"

Was it the kind of information you're looking for ? If not, please explain.

Bruno

P.S. The syntax given is MySQL specific, but it should work all the same. If you try to create a link in Access, then look at the SQL code generated, you should see something like INNER JOIN or OUTER JOIN, but the JOIN keyword is the important one, that will guide you to the correct SQL syntax.

--- Better to die trying, than never try at all ---

Reply to this topic