Tenderfoot :
The following query with left joins returns records that do not have a match on the final left join, i.e. the assessment column is null, though I have the query with IS NOT NULL
.
How should the query be changed to get just the matching records?
Basically I want to return unit_outcome records GROUPED according to related assessments in the lookup table.
DB Fiddle for LEFT JOIN showing records with null for assessment
SELECT *
FROM unit
left JOIN unit_unit_outcome_lookup
ON unit_unit_outcome_lookup.unit_fk = unit.unit_pk
left JOIN unit_outcome
ON unit_outcome.unit_outcome_pk = unit_unit_outcome_lookup.unit_outcome_fk
left JOIN unit_outcome_assessment_lookup
ON unit_outcome_assessment_lookup.unit_outcome_fk = unit_outcome.unit_outcome_pk IS NOT NULL
left JOIN assessment
ON assessment.assessment_pk = unit_outcome_assessment_lookup.assessment_fk IS NOT NULL
AND unit.unit_pk ='1'
DB Fiddle for INNER JOIN with no null records - how to group unit_outcomes by assessment?
Akina :
SELECT MAX((unit.unit_pk)) AS unit_pk,
GROUP_CONCAT(unit_outcome.unit_outcome) unit_outcomes,
MAX(assessment.assessment) assessment,
GROUP_CONCAT(unit_outcome.unit_outcome_pk) unit_outcome_pks,
assessment.assessment_pk
FROM unit
INNER JOIN unit_unit_outcome_lookup
ON unit_unit_outcome_lookup.unit_fk = unit.unit_pk
INNER JOIN unit_outcome
ON unit_outcome.unit_outcome_pk = unit_unit_outcome_lookup.unit_outcome_fk
INNER JOIN unit_outcome_assessment_lookup
ON unit_outcome_assessment_lookup.unit_outcome_fk = unit_outcome.unit_outcome_pk
INNER JOIN assessment
ON assessment.assessment_pk = unit_outcome_assessment_lookup.assessment_fk
AND unit.unit_pk ='1'
GROUP BY assessment_pk;
PS. Details can be found in the comments to the question.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=237642&siteId=1