MYSQL 一个大左外连接或 3 个左外连接相同的表?

MYSQL one big left outer join or 3 left outer joins same tables?

我不确定如何将两个 table 连接到我的主查询中。确定帐号的两个 table 连接到 3 个主要 table 中的每一个,以应对不同的情况。

我正在尝试根据三件事来确定帐号。 帐号基于支付代码、部门和员工类型,这是三个不同的 table。

以下是它们的连接方式。

/* all accountnumbers have a paycode_id*/
accountcodes.paycode_id = employee_pay.id
/* all accountnumbers are assigned to either a certain department or all*/
accountcodes.department_code = department.code
/* the accountnumber can be assigned to one emp_type or both*/
accountcodes.emp_type_id = employee_infos.emp_type_id
/* the accountnumber is in table:lkp_accountcodes, that determines the account number table:accountcodes */
accountcodes.lkp_accountcodes_id = lkp_accountcodes.id

table:账户代码

 -----------------------------------------------------------------------
| 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 |  accountnumber |
|-----|------------|----------------|
|  21 |   500      |      5210      |
|  22 |   500      |      6210      |
|  23 |   500      |      2211      |
|  24 |   500      |      2210      |
|  25 |   500      |      5010      |
|  26 |   500      |      6000      |
|  27 |   500      |      5090      |
 -----------------------------------

我不知道我是否应该进行三个左联接、创建临时 table 或像下面这样的一个大左外部联接? 另外,我不确定如何对其进行分组,如果部门代码为空,则帐号应由 paycode_id 和 emp_type_id 确定。 帮我解决下面的查询。

    SELECT i.employee, d.department, e1.paycode, a1.accountnumber
    FROM employee_pay e1
    INNER JOIN employee_infos i ON e1.emp_info_id = i.id
    INNER JOIN department d ON i.department_id = d.id
    LEFT OUTER JOIN accountcodes ac ON ac.paycode_id = e1.id 
        AND ac.emp_type_id = i.emp_type_id 
        AND ac.department_code = d.code -- if null? 
    LEFT OUTER JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id
    -- group? 

预期结果

emp_number | emp_type | deptartment | pay_code | account_number
123        | temp     | 120         | CPP Ded  | 5210
456        | reg      | 310         | CPP Ded  | 6210
789        | temp     | null        | ExpReim  | 2210
987        | reg      | null        | ExpReim  | 2211
654        | reg      | 145         | StatHol  | 5010
321        | temp     | 145         | StatHol  | 5090
333        | temp     | 532         | StatHol  | 6000

这个应该没问题

SELECT i.employee, d.department, e1.paycode, a1.accountnumber
    FROM employee_pay e1
    JOIN employee_infos i ON e1.emp_info_id = i.id
    JOIN department d ON i.department_id = d.id
    JOIN accountcodes ac ON ac.paycode_id = e1.id 
        AND ac.emp_type_id = i.emp_type_id 
        AND ac.department_code = d.code -- if null? 
    LEFT OUTER JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id;

如果你想避免重复行而不是 GROUP BY,那么你应该使用 DISTINCT

  SELECT DISTINCT i.employee, d.department, e1.paycode, a1.accountnumber
  FROM employee_pay e1
  INNER JOIN employee_infos i ON e1.emp_info_id = i.id
  INNER JOIN department d ON i.department_id = d.id
  LEFT  JOIN accountcodes ac ON ac.paycode_id = e1.id 
            AND ac.emp_type_id = i.emp_type_id 
            AND ac.department_code = d.code 
  LEFT  JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id