Skip to main content
Inspiring
June 15, 2006
Answered

More than two INNER JOINS

Below I'm inserting a code example that I would like to know if the syntax is correct for the third JOIN. I know that it works for two JOINS. My question is if you have more than two, you just keep on adding like the line of the third JOIN.

<cfquery name="query1" datasource"database">
SELECT column1, column2, column3, t1.XXID, t2.YYID, t3.ZZ1ID
FROM (MainTable mt INNER JOIN Table1 t1 ON mt.XXID = t1.XXID)
INNER JOIN Tabel2 t2 ON mt.YYID = t2.YYID
INNER JOIN Tabel3 t3 ON mt.ZZID = t3.ZZID
</cfquery>

Also for the next example

<cfquery name="query1" datasource"database">
SELECT column1, column2, column3, t1.XXID, t1.YYID, t1.ZZ1ID
FROM (MainTable mt INNER JOIN Table1 t1 ON mt.XXID = t1.XXID)
INNER JOIN Tabel1 t1 ON mt.YYID = t1.XXID
INNER JOIN Tabel1 t1 ON mt.ZZID = t1.XXID
</cfquery>

Thanks
Manuel Lino
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer
June 18, 2006
The best way to do this is to set up three tables:
(1) A jobs table that just has job related info.
(2) An Employee table, that just has employee info.
(3) And a relation table that links employees to jobs. This is part of a "normalized" design and will save you much grief later.

See the attached code.

4 replies

June 17, 2006
First, for questions like this, please state which RDBMS you are using because the syntax varies -- especially with MS Access.

Second, the "next example" is not valid because it declares t1 more than once.

The first example is OK except that it has parenthesis that may cause an error and it has some unscoped columns.

Use the attached (SQL standard form; works for most, not all, RDBMS):

Translate
Inspiring
June 17, 2006
Thank you so much for the reply MikerRoo,

First I´m using Microsoft SQL Server 2000.
Second:
The next example was actually the one I needed to use. Maybe or for sure I´m not thinking properly or thinking the correct approach. I´ll explain, as maybe you can help me approach correctly this problem. The mt or Main Table refers to the main tabel where Jobs are defined. These jobs must identify the employees that are scheduled to work on it. Might be 1... to 12. Also I need to pevent the schedulling of an employee to a certain job, if that Employee is already scheduled to another one on the period being specified in the new entry.
I thought that by having up to 12 fields in the main table related to the employee table, I could in the future query any situation like I mentioned above. Is the solution having 12 different queries instead of 1 with 12 inner joins.
Again thanks for your time.
Manuel
Translate
Correct answer
June 18, 2006
The best way to do this is to set up three tables:
(1) A jobs table that just has job related info.
(2) An Employee table, that just has employee info.
(3) And a relation table that links employees to jobs. This is part of a "normalized" design and will save you much grief later.

See the attached code.

Translate