简体   繁体   English

MySQL:如何从一个表中选择和显示所有行,并计算另一个表中where子句的总和?

[英]MySQL: How to select and display ALL rows from one table, and calculate the sum of a where clause on another table?

I'm trying to display all rows from one table and also SUM/AVG the results in one column, which is the result of a where clause. 我正在尝试显示一个表中的所有行,并将结果SUM / AVG放在一列中,这是where子句的结果。 That probably doesn't make much sense, so let me explain. 这可能没什么意义,所以让我解释一下。

I need to display a report of all employees... 我需要显示所有员工的报告......

SELECT Employees.Name, Employees.Extension 
FROM Employees;

--------------
| Name | Ext |
--------------
| Joe  | 123 |
| Jane | 124 |
| John | 125 |
--------------

...and join some information from the PhoneCalls table... ...并加入来自PhoneCalls表的一些信息......

--------------------------------------------------------------
| PhoneCalls Table                                           |
--------------------------------------------------------------
| Ext |      StartTime      |       EndTime       | Duration |
--------------------------------------------------------------
| 123 | 2010-09-05 10:54:22 | 2010-09-05 10:58:22 |   240    |
--------------------------------------------------------------

SELECT Employees.Name, 
       Employees.Extension,
       Count(PhoneCalls.*) AS CallCount, 
       AVG(PhoneCalls.Duration) AS AverageCallTime, 
       SUM(PhoneCalls.Duration) AS TotalCallTime
FROM Employees
LEFT JOIN PhoneCalls ON Employees.Extension = PhoneCalls.Extension
GROUP BY Employees.Extension;

------------------------------------------------------------
| Name | Ext | CallCount | AverageCallTime | TotalCallTime |
------------------------------------------------------------
| Joe  | 123 |     10    |       200       |      2000     |
| Jane | 124 |     20    |       250       |      5000     |
| John | 125 |      3    |       100       |       300     |
------------------------------------------------------------

Now I want to filter out some of the rows that are included in the SUM and AVG calculations... 现在我想过滤掉SUM和AVG计算中包含的一些行...

WHERE PhoneCalls.StartTime BETWEEN "2010-09-12 09:30:00" AND NOW()

...which will ideally result in a table looking something like this: ...理想情况下会导致表格看起来像这样:

------------------------------------------------------------
| Name | Ext | CallCount | AverageCallTime | TotalCallTime |
------------------------------------------------------------
| Joe  | 123 |      5    |       200       |      1000     |
| Jane | 124 |     10    |       250       |      2500     |
| John | 125 |      0    |         0       |         0     | 
------------------------------------------------------------

Note that John has not made any calls in this date range, so his total CallCount is zero, but he is still in the list of results. 请注意,John没有在此日期范围内进行任何调用,因此他的总CallCount为零,但他仍然在结果列表中。 I can't seem to figure out how to keep records like John's in the list. 我似乎无法弄清楚如何在列表中保留像John这样的记录。 When I add the WHERE clause, those records are filtered out. 当我添加WHERE子句时,这些记录被过滤掉。

How can I create a select statement that displays all of the Employees and only SUMs/AVGs the values returned from the WHERE clause? 如何创建一个显示所有Employees的select语句,并且只显示从WHERE子句返回的值的SUMs / AVG?

Use: 使用:

   SELECT e.Name, 
          e.Extension,
          Count(pc.*) AS CallCount, 
          AVG(pc.Duration) AS AverageCallTime, 
          SUM(pc.Duration) AS TotalCallTime
     FROM Employees e
LEFT JOIN PhoneCalls pc ON pc.extension = e.extension
                       AND pc.StartTime BETWEEN "2010-09-12 09:30:00" AND NOW()
 GROUP BY e.Name, e.Extension

The issue is when using an OUTER JOIN, specifying criteria in the JOIN section is applied before the JOIN takes place--like a derived table or inline view. 问题是当使用OUTER JOIN时,在JOIN发生之前应用指定JOIN部分中的条件 - 就像派生表或内联视图一样。 The WHERE clause is applied after the OUTER JOIN, which is why when you specified the WHERE clause on the table being LEFT OUTER JOIN'd to that the rows you still wanted to see are being filtered out. 在OUTER JOIN之后应用WHERE子句,这就是为什么当您在表上指定WHERE子句为LEFT OUTER JOIN时,您仍希望看到的行被过滤掉的原因。

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

相关问题 获取一个表中的所有行,使用where子句对第二个表中的列求和 - get all rows from one table, sum on column from a second table using a where clause 如何使用 WHERE 子句使用表本身和 MySQL 中的另一个表的值总和来更新表 - How to update a table with sum of values from the table itself and another table in MySQL with WHERE clause 显示一个表中的所有记录和另一个表中的行总和 - to display all records from one table and sum of rows from another table 从一个表计算 SUM 并显示另一个表的列 - Calculate SUM from one table and display columns from another 从一个表中选择在另一个表中存在映射的行 - Select rows from one table where mapping exists in another table 在一个查询中从单个mysql表中选择行和总和 - select rows and a sum from a single mysql table in one query 如何从表中匹配两列 where 子句的 select 行? - How to select rows from table that matches where clause on two columns? MySQL - 如果在另一个表上匹配,则从一个表中排除所有行 - MySQL - Exclude all rows from one table if match on another table 使用where子句从另一个表中选择数据 - Select data from another table with where clause mysql-从一个表中选择所有内容,并在其中找到$ var的另一列中选择所有内容 - mysql - Select all from one table and one column form another where $var is found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM