简体   繁体   English

在三个不同的条件下,将同一张表连接到多个表。 MySQL的

[英]Join same tables to multiple tables, three different conditions. mysql

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. 我不确定如何将两个表联接到我的主查询上,因为针对不同的情况它们分别联接到3个主表中。

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 表格: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 选择* from accountcodes内部联接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? 我不知道应该做三个左联接还是创建临时表?

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. 不确定如何将其分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM