Using MySQl 5.6 as my database, I'm trying to join a table twice, on another joined table, but I'm getting duplicate results, rather than the row combined.
SO, I have a checks table, and a check can be assigned to an area or areas, which are stored in the check_assigned_area table. Entries in this table can either be a location or a department. Locations being a parent of departments.
SO, im joining the checks_assigned_area table on to the checks table via the PK (check_id)
AND then im trying to join the areas table twice, once for locations and once for departments, depending on the area_level. 3 being a location and 4 being a department.
But instead of the query returning a single row with the location name and department name. Its returning 2 seperate rows, and it isnt populating the department name.
I've tried all manner of left/inner/right join combinations, distincts, group by's, but I cant get the data correct.
Can I even do it like this? My logic tells me yes, but practically I cant get it to work.
Any ideas if I can get it to output the data i need?
Heres an SQL fiddle of my data and query http://sqlfiddle.com/#!9/63d6ce/4
Checks table:
+----------+---------------+------------+------------+--------+----------+ | check_id | check_type_id | due | created_by | status | sign_off | +----------+---------------+------------+------------+--------+----------+ | 19335 | 43 | 18/09/2017 | 3 | 1 | 1 | | 19336 | 4 | 19/09/2017 | 3 | 1 | 1 | | 19358 | 62 | 21/09/2017 | 47336 | 1 | 1 | +----------+---------------+------------+------------+--------+----------+
Check assigned area table:
+------------------------+---------------+----------+ | check_assigned_area_id | check_area_id | check_id | +------------------------+---------------+----------+ | 18801 | 9 | 19335 | | 18802 | 816 | 19336 | | 18803 | 816 | 19336 | | 18804 | 8091 | 19358 | | 18835 | 8092 | 19358 | +------------------------+---------------+----------+
Query:
SELECT `c`.*, `t`.`check_name`,`a`.`check_area_id`,`l`.`area_name` AS 'location_name', `d`.`area_name` AS 'dept_name'
FROM `checks` `c`
LEFT JOIN `check_assigned_areas` `a` ON `a`.`check_id` = `c`.`check_id`
LEFT JOIN `areas` `l` ON `l`.`area_id` = `a`.`check_area_id` AND `l`.`area_level` = '3'
LEFT JOIN `areas` `d` ON `d`.`area_id` = `a`.`check_area_id` AND `d`.`area_level` = '4'
LEFT JOIN `check_types` `t` ON `t`.`check_type_id` = `c`.`check_type_id`
WHERE `c`.`org_id` = '297'
AND `c`.`status` != '0'
AND `c`.`due` <= '2017-09-21'