MikisaMaliDojnik
MikisaMaliDojnik

Reputation: 87

Multiple joins in SQL statement

To clear things up, I have two tables, employees and jobs.

In employees I have columns firstname, lastname, employees id.

In jobs I have columns

job_id, job_name, job_description, job_opener, job_responsible_person

where the last two are foreign keys which refer to the employees table.

This is my SQL statement

String cmd = "SELECT je.id, firstname, lastname, job_name FROM jat.employees je" +
            " INNER JOIN jat.jobs jj ON je.id = jj.job_opener ";

And I fill data table with it.

What I need is adding two more columns, like second first name and second last name, in same table, but which will depend on job_responsible_p, so ON clause would be

ON je.id = jj.job_responsible_p

Is that possible in one query?

Upvotes: 1

Views: 72

Answers (2)

Eugene Primako
Eugene Primako

Reputation: 2817

Yes, you can join several table at once:

SELECT je.id, je.firstname, je.lastname, jj.job_name, jr.firstname, jr.lastname
FROM jat.employees AS je INNER JOIN jat.jobs AS jj INNER JOIN jat.employees AS jr
ON je.id=jj.job_opener AND jr.id=jj.job_responsible_person;

Upvotes: 1

Rahul
Rahul

Reputation: 77866

Yes it is since you can use conditional operator like AND OR with JOIN ON condition.

INNER JOIN jat.jobs jj 
ON je.id=jj.job_opener 
AND je.id=jj.job_responsible_p

Upvotes: 0

Related Questions