Reputation: 33
I'm not sure how to join two tables onto my main query because they each join to each of the 3 main tables for different situations.
I'm trying to determine the accountcode based on three things. see main tables below. An accountcode is based on a paycode, department and employee type which are three different tables.
accountcodes.paycode_id = employee_pay.paycode_id
accountcodes.department_code = department.code
accountcodes.emp_type_id = employee_infos.emp_type_id
Here is how I join these tables
SELECT i.id, i.emp_type_id, m.code as deptartment
FROM employee_pay e1
INNER JOIN employee_infos i ON e1.employee_infos_id = i.id
INNER JOIN department m ON i.co_cost_center_matrix_id = m.id
I need to join the next two tables on many different conditions.
table:accountcodes
-----------------------------------------------------------------------
| ID | lkp_accountcodes_id | paycode_id | department_code | emp_type_id |
|--------------------------|------------|-----------------|-------------|
| 1 | 21 | 15 | 120 | 1 |
|--------------------------|------------|-----------------|-------------|
| 2 | 22 | 15 | 310 | 1 |
|--------------------------|------------|-----------------|-------------|
| 3 | 23 | 30 | null | 1 |
|--------------------------|------------|-----------------|-------------|
| 4 | 24 | 30 | null | 2 |
|--------------------------|------------|-----------------|-------------|
| 5 | 25 | 55 | 120 | 1 |
|--------------------------|------------|-----------------|-------------|
| 6 | 26 | 55 | 310 | 2 |
|--------------------------|------------|-----------------|-------------|
| 7 | 27 | 55 | 120 | 2 |
-----------------------------------------------------------------------
table:lkp_accountcodes
-----------------------------------
| id | company_id | accountcode |
|-----|------------|----------------|
| 21 | 500 | 5210 |
|-----|------------|----------------|
| 22 | 500 | 6210 |
|-----|------------|----------------|
| 23 | 500 | 2211 |
|-----|------------|----------------|
| 24 | 500 | 2210 |
|-----|------------|----------------|
| 25 | 500 | 5010 |
|-----|------------|----------------|
| 26 | 500 | 6000 |
|-----|------------|----------------|
| 27 | 500 | 5090 |
-----------------------------------
select * from accountcodes a inner join lkp_accountcodes lac on a.lkp_accountcodes_id = lac.id
I don't know if I should be doing three left joins or create temporary tables?
Upvotes: 0
Views: 33
Reputation: 33
I doubt this is right but did you try
SELECT i.id, i.emp_type_id, m.code as deptartment
FROM employee_pay e1
INNER JOIN employee_infos i ON e1.employee_infos_id = i.id
INNER JOIN department m ON i.co_cost_center_matrix_id = m.id
/* paycode */
LEFT OUTER JOIN accountcodes a1 ON a1.paycode_id = e1.paycode_id
LEFT OUTER JOIN lkp_accountcodes ac1 on a1.lkp_accountcodes_id = ac1.id
/* department */
LEFT OUTER JOIN accountcodes a2 ON a2.department_code = m.code
LEFT OUTER JOIN lkp_accountcodes ac2 on a2.lkp_accountcodes_id = ac2.id
/* employee type */
LEFT OUTER JOIN accountcodes a3 ON a3.emp_type_id = i.emp_type_id
LEFT OUTER JOIN lkp_accountcodes ac3 on a3.lkp_accountcodes_id = ac3.id
not sure how you would group this though.
Upvotes: 0