gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:40:00 26 4
gpt4 key购买 nike

套件

+------------------+--------------+------+-----+---------+----------------+
| 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 | |------------------+--------------+------+-----+---------+----------------+

文档

+---------------------+-------------+------+-----+---------+----------------+
| 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 | +---------------------+-------------+------+-----+---------+----------------+

服务

+-------------------+--------------+------+-----+---------+----------------+
| 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 | +-------------------+--------------+------+-----+---------+----------------+

键_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 张 table 。我想要这些字段的结果:

 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) 作为“DOC”,SUM(srv.srv_cheque) 作为“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的key与SRV和DOC的key不同,结果重复。我不知道如何将 SRV 和 DOC 的 key 与 KIT 的 key 连接起来。结构有点乱,也许使用“加入”会解决它,但我不知道如何解决。我使用“加入”而不是这个内部选择做了一些查询,但到目前为止没有成功

最佳答案

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

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

关于mysql - 如何将 3 个表合并为一个表,这些表具有到第四个表的外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com