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

时间:2017-09-23 18:51:24

标签: mysql group-by left-join ifnull multiple-join-rows

我不确定如何将两个表连接到我的主查询上。确定帐户编号的两个表连接到不同情况的3个主表中的每一个。

我正在尝试根据三件事确定帐号。 帐户号码基于付费代码,部门和员工类型,它们是三个不同的表格。

以下是他们应该加入的方式。

/* 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

表: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      |
 -----------------------------------------------------------------------

表: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      |
 -----------------------------------

我不知道我是否应该做三个左连接,创建临时表,或者像下面的一个大的左外连接?  此外,我不确定如何对其进行分组,如果离开代码为null,则应该通过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

2 个答案:

答案 0 :(得分:0)

这应该没问题。

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;

答案 1 :(得分:0)

如果您想要避免重复行而不是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