Skip to content
Advertisement

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?

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement