具有多个表的自定义MySql查询

问题描述 投票:-2回答:1

我在从这些表中提取数据时遇到问题。我有一个统计系统,每个月中的每一天都会为员工输入数字-举例来说,这里只有两个月,但这将是一整年。我尝试加入,但结果却翻了一番,而且有些员工可以使用更多帐户,而其他员工只能使用一个...

员工

+----+-------------+--+
| ID |    Name     |  |
+----+-------------+--+
|  1 | John Doe    |  |
|  2 | Helen Price |  |
|  3 | John Price  |  |
+----+-------------+--+

帐户

+----+---------+--+
| ID |  Name   |  |
+----+---------+--+
|  1 | fisher  |  |
|  2 | fisher1 |  |
+----+---------+--+

一月

+----+-------------+---------+----------+----------+----------+
| ID |    Employee | Account | firstday | seconday | thirdday |
+----+-------------+---------+----------+----------+----------+
|  1 | John Doe    | fisher  |        2 |        4 |        6 | 
|  2 | Helen Price | fisher  |        1 |        1 |        1 | 
|  3 | John Price  | fisher1 |        1 |        2 |        1 | 
|  4 | John Price  | fisher  |        1 |        1 |        1 | 
+----+-------------+---------+----------+----------+----------+

2月

+----+-------------+---------+-----------+----------+-----------+
| ID |    Employee | Account | fourthday | eightday | secondday | 
+----+-------------+---------+-----------+----------+-----------+
|  1 | John Doe    | fisher  |         1 |        4 |         6 |  
|  2 | Helen Price | fisher  |         1 |        1 |         2 |  
|  3 | John Price  | fisher1 |         1 |        2 |         1 |   
|  4 | John Price  | fisher  |         1 |        1 |         1 |    
+----+-------------+---------+-----------+----------+-----------+

我的问题是如何编写具有以下结果的查询:

   +----+-------------+---------+-----------------+------------------+--------+--+
| ID |  Employee   | Account | January(totals) | February(totals) | Totals |  |
+----+-------------+---------+-----------------+------------------+--------+--+
|  1 | John Doe    | fisher  |              12 |               11 |     23 |  |
|  2 | Helen Price | fisher  |               3 |                4 |      7 |  |
|  3 | John Price  | fisher  |               3 |                3 |      6 |  |
+----+-------------+---------+-----------------+------------------+--------+--+
mysql pivot-table
1个回答
0
投票

您想要的是来自不同JOIN表的汇总结果的数据透视表。有两种方法,但一种方法是将LEFT JOIN与子查询汇总使用,以汇总每个员工和每个帐户的月度结果。

在您的样本数据中,[Month]表之外没有EmployeeAccount的关联。因此,需要通过使用另一个子查询来创建关联来创建关联。除非创建用于创建关联的多对多表,否则这将导致每个员工和每个帐户都有关联。

最后,由于您的示例数据仅显示单个帐户(fisher,因此您需要以某种方式对其进行过滤,这在您的问题中并未明确指定,因此我将其添加到WHERE标准中。

示例:DB-Fiddle

SELECT
  e.ID,
  e.Name AS Employee,
  e.Account,
  COALESCE(janurary_totals.total, 0) AS `January(totals)`,
  COALESCE(february_totals.total, 0) AS `February(totals)`,
  COALESCE(janurary_totals.total, 0) + COALESCE(february_totals.total, 0) AS Totals
FROM (
   SELECT e1.ID, e1.Name, a1.Name AS Account
   FROM employees AS e1
   LEFT JOIN Account AS a1
   ON a1.ID IS NOT NULL
) AS e
LEFT JOIN (
   SELECT
       Employee,
       Account,
       SUM(firstday) + SUM(seconday) + SUM(thirdday) AS total
   FROM January
   GROUP BY Employee, Account
) AS janurary_totals
ON janurary_totals.Employee = e.Name
AND janurary_totals.Account = e.Account
LEFT JOIN (
   SELECT
       Employee,
       Account,
       SUM(fourthday) + SUM(eightday) + SUM(secondday) AS total
   FROM February
   GROUP BY Employee, Account
) AS february_totals
ON february_totals.Employee = e.Name
AND february_totals.Account = e.Account
WHERE e.Account = 'fisher'

结果

| ID  | Employee    | Account | January(totals) | February(totals) | Totals |
| --- | ----------- | ------- | --------------- | ---------------- | ------ |
| 1   | John Doe    | fisher  | 12              | 11               | 23     |
| 2   | Helen Price | fisher  | 3               | 4                | 7      |
| 3   | John Price  | fisher  | 3               | 3                | 6      |

您可以通过将各个月份的表重构为带有DATETIME列的单个employee_entry表,从而极大地简化事情,您可以使用该表查询创建数据透视表。

© www.soinside.com 2019 - 2024. All rights reserved.