如何将3个表合并为一个表,而这些表具有第四个表的外键?

时间:2018-08-28 21:18:19

标签: mysql sql dbeaver

工具包

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          +------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment | protocol         | varchar(100) | YES  | UNI | NULL    |                | detail_service   | varchar(120) | YES  |     | NULL    |                | date_conclusion  | date         | YES  |     | NULL    |                | resp_key         | varchar(10)  | YES  | MUL | NULL    |                |------------------+--------------+------+-----+---------+----------------+

doc

+---------------------+-------------+------+-----+---------+----------------+
| Field               | Type        | Null | Key | Default | Extra          |
+---------------------+-------------+------+-----+---------+----------------+
| id                  | int(11)     | NO   | PRI | NULL    |auto_increment | employee_key        | varchar(10) | YES  | MUL | NULL    |                | doc_total           | int(11)     | YES  |     | NULL    |                | date_doc            | date        | YES  |     | NULL    |                +---------------------+-------------+------+-----+---------+----------------+

srv

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          +-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment | employee_key      | varchar(10)  | YES  | MUL | NULL    |                | srv_total         | int(11)      | YES  |     | NULL    |                | date_srv          | date         | YES  |     | NULL    |                +-------------------+--------------+------+-----+---------+----------------+

key_e

+-------------------+--------------+------+-----+---------+-------+
| Field             | Type         | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| key               | varchar(10)  | NO   | PRI | NULL    |       |
| login_employee    | varchar(100) | YES  | MUL | NULL    |       |
+-------------------+--------------+------+-----+---------+-------+

员工

+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| login      | varchar(100) | NO   | PRI | NULL    |       |
| name       | varchar(100) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+

我有这5张桌子。我想要类似以下字段的结果:

 employee.login, employee.name, SUM(kit.detail_service LIKE '%Deal%') AS 'Deal', SUM(kit.detail_service LIKE '%Not Deal%') AS 'Not Deal', SUM(kit.detail_service LIKE '%Postponed%') AS 'Postponed',

SUM(doc.doc_total)AS'DOC',SUM(srv.srv_cheque)AS'SRV'

key_e.k:employee.login的外键 kit.resp_key:key_e的外键 doc.employee_key / srv.employee_key:key_e的外键(这两个都相等,但kit.resp_key不同)

我进行的查询:

SELECT
    e.login,
    e.name,
    (SELECT SUM(k.detail_service LIKE '%Deal%') FROM kit k WHERE k.resp_key=key_e.key) AS 'Deal',
    (SELECT SUM(k.detail_service LIKE '%Not Deal%') FROM kit k WHERE k.resp_key=key_e.key) AS 'Not Deal',
    (SELECT SUM(k.detail_service LIKE '%Postponed%') FROM kit k WHERE k.resp_key=key_e.key) AS 'Postponed',
    (SELECT SUM(doc_total) FROM doc WHERE doc.employee_key=key_e.key) AS 'DOC',
    (SELECT SUM(srv_total) FROM srv WHERE srv.employee_key=key_e.key) AS 'SRV'
FROM
    employee e
INNER JOIN key_e ON
    e.login = key_e.login_employee
GROUP BY e.login, key_e.key

======================================

我所做的查询几乎返回了我想要的一切,除了重复的行。由于KIT的密钥以及SRV和DOC的密钥不同,因此结果重复。我不知道如何将SRV和DOC的密钥与KIT的密钥合并。该结构有点混乱,也许使用“ join”会解决它,但我不知道如何。我使用“ join”而不是这个内部选择进行了一些查询,但到目前为止没有成功

1 个答案:

答案 0 :(得分:0)

我认为您需要两个聚合级别。特别是,您要在进行连接之前在键级别 进行聚合。然后,您想在登录级别进行聚合:

SELECT e.login, e.name,
       SUM(kit.num_Deal) as num_Deal,
       SUM(kit.num_not_deal) as num_not_deal,
       SUM(kit.num_Postponed) as num_Postponed,
       SUM(doc_total) as doc_total,
       SUM(srv_total) as srv_total
FROM employee e INNER JOIN
     key_e ON
     ON e.login = key_e.login_employee LEFT JOIN
     (SELECT doc.employee_key, SUM(doc.doc_total) as doc_total
      FROM doc
      GROUP BY doc.employee_key
     ) doc
     ON doc.employee_key = key_e.key LEFT JOIN
     (SELECT srv.employee_key, SUM(srv.srv_total) as srv_total
      FROM srv
      GROUP BY srv.employee_key
     ) srv
     ON srv.employee_key = key_e.key LEFT JOIN
     (SELECT kit.employee_key, 
             SUM(k.detail_service LIKE '%Deal%') as num_Deal,
             SUM(k.detail_service LIKE '%Not Deal%') as num_not_deal,
             SUM(k.detail_service LIKE '%Postponed%') as Postponed
      FROM kit k
      GROUP BY kit.employee_key
     ) kit
     ON kit.resp_key = key_e.key
GROUP BY e.login, e.name