[英]MySQL query not getting the desired result
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". 我正在尝试以他的“ functienaam”为基础,获取所有为2的“ afdelingen”和“ afdelingen.naam”后面的“ director”的名称。 But I only get the "afdelingen.naam" with the name of the "Directeur". 但是我只能得到名称为“ Directeur”的“ afdelingen.naam”。
How do I also get the other "afdelingen.naam" in this result. 如何在此结果中获得另一个“ afdelingen.naam”。
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
Current result: Disign & Ontwikkeling | 当前结果:Disign&Ontwikkeling | Wilhelmus Frederikus Aloisius | Wilhelmus Frederikus Aloisius | van | 货车| Hot naar Her | 热纳尔她| Directeur DIRECTEUR
Desired result: 所需结果:
Left joins as mentioned by @rickdenhaan are only part of it; @rickdenhaan提到的左联接只是其中的一部分; you're also filtering for rollen.id = 1
. 您还将过滤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. 由于“ Verkoop&Administratief”复杂性(我认为这是正确的单数?)没有Directeur,因此您要使用WHERE
子句从结果集中删除该行。
Instead of filtering with a WHERE
, add the filter to your join condition so you only join the Directeur role. 无需使用WHERE
进行过滤,而是将过滤器添加到您的加入条件中,以便仅加入Directeur角色。 And use LEFT JOIN
s to ensure that you join nothing if there is no Directeur instead of removing rows from your dataset without the corresponding role. 并使用LEFT JOIN
来确保没有Directeur时不加入任何内容,而不是从没有相应角色的数据集中删除行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.