I have 3 tables which are Department, Office, Division
Supposed flow is: Department has Offices has Divisions
But there are instances that Divisions reports directly to Departments
Example Data
Department Office Division
[id][name] [dept_id][id][name] [dept_id][off_id][id][name]
[ 1][A ] [ 1 ][ 1][D ] [ 1 ][ 2 ][ 1][G ]
[ 2][B ] [ 1 ][ 2][E ] [ 3 ][ NULL ][ 2][H ]
[ 3][C ] [ 3 ][ 3][F ] [ 3 ][ 3 ][ 3][I ]
Desired Output to display all Divisions:
OUTPUT
[AS dept_name][AS off_name][AS div_name]
[ A ][ E ][ G ]
[ C ][ NULL ][ H ]
[ C ][ F ][ I ]
My current Query is:
SELECT * FROM `Division` A
RIGHT JOIN `Office` B ON A.`off_id` = B.`id`
RIGHT JOIN `Department` C ON A.`dept_id` = C.`id`;
my query is wrong, so i'm looking for a way to join those 3 table to achieve the desired output