I'm trying to use GROUP_CONCAT to combine results within my query. Basically, the result I want is this:
Code:
agency_number | case_type_id | defatty | prosatty
--------------+--------------+-------------+-----------------
2010-25532 | standard | 16,21,163 | 18,56,63
Here my query:
Code:
select cases_details.agency_number, cases_details.case_type_id,
group_concat(distinct def_rel.people_id) as defatty, group_concat(distinct pros_rel.people_id) as prosatty
from cases_details
left join cases_relationships as def_rel on cases_details.id = def_rel.cases_details_id
left join cases_relationships as pros_rel on cases_details.id = pros_rel.cases_details_id
where pros_rel.role_id = 'prosecutor' and def_rel.role_id = 'defense'
group by agency_number, case_type_id
I have three tables basically.
cases_details holds a broad range of information about a single case (case number, case type, etc.).
people holds detailed information about people (last name, first name, etc.).
cases_relationships holds the relationship for people who are involved in a case.
My problem is, my query works for the most part. The correct information is returned. The problem I'm having is if a case does not contain any
defattys or
prosattys, then the cases are not included in the results. I thought
null would be returned, but that doesn't seem to be the case. What am I doing wrong? Thanks!