1

I am trying to get all the "afdelingen" which are 2 and the name of the "directeur" behind the "afdelingen.naam" base on his "functienaam". But I only get the "afdelingen.naam" with the name of the "Directeur".

How do I also get the other "afdelingen.naam" in this result.

This is what I currently have:

SELECT afdelingen.naam, personeel.voornamen, personeel.tussenvoegsels, personeel.achternaam, rollen.functienaam
FROM `afdelingen` INNER JOIN
     personeel_afdeling
     ON afdelingen.id = personeel_afdeling.afdeling_id INNER JOIN 
     personeel
     ON personeel_afdeling.personeel_id = personeel.id INNER JOIN
     rollen
     ON personeel_afdeling.rol_id = rollen.id
WHERE rollen.id LIKE 1

A quick overview of the tables that are used

Current result: Design & Ontwikkeling | Wilhelmus Frederikus Aloisius | van | Hot naar Her | Directeur

Desired result:

  • Verkoop & Administratief
  • Design & Ontwikkeling | Wilhelmus Frederikus Aloisius | van | Hot naar Her | Directeur
3
  • 1
    You need to provide sample data and desired results. Your description is a bit difficult because of the language issue. Commented Nov 19, 2017 at 16:26
  • Inner joins are exclusive; if there is no result in one of the joined tables, you won't get a row in your result. What happens if you use left joins instead? Commented Nov 19, 2017 at 16:42
  • @rickdenhaan Left joins give the same results Commented Nov 19, 2017 at 16:49

1 Answer 1

1

Left joins as mentioned by @rickdenhaan are only part of it; you're also filtering for rollen.id = 1. Since the "Verkoop & Administratief" afdeling (I think that's the right singular?) does not have a Directeur, you're eliminating that row from your result set with the WHERE clause.

Instead of filtering with a WHERE, add the filter to your join condition so you only join the Directeur role. And use LEFT JOINs to ensure that you join nothing if there is no Directeur instead of removing rows from your dataset without the corresponding role.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.