php - MYSQL 多重计数和求和

标签 php mysql sql join group-by

我希望这在 MYSQL 中是可能的,我正在使用 PHP 编写脚本。

我正在尝试根据每个月的各个条件和分组,在 table1 上的值和 COUNT 上创建多个列。这些表已通过 accountid 连接。 我有两张表月度报告(表1)和花盆(表2)。

预期结果见表 1

月度报告(表1)

REPORTID|ACCOUNTID|COMPMONTH|SUMtoDATE|COUNTtoDATE|SUMcompDATE|COUNTcompDATE|
1     |   190     |    JAN    |   150     |      2      |    150      |       2       | 
2     |   190     |    FEB    |     0     |      0      |    100      |       1       |

花盆(表2)

PlanterID | ACCOUNTID |PLANTER |  SALARY |  compDATE  |    toDATE   |
1         |    190    |   aaa  |   100   | Jan-1-2013 | Jan-05-2013 |
2         |    190    |   bbb  |    50   | Jan-9-2013 | Jan-12-2013 |
3         |    190    |   aaa  |   100   | Feb-1-2013 | Mar-12-2013 |
4         |    190    |   bbb  |     0   | Mar-5-2013 | Mar-12-2013 |

带有内部联接的单个查询已经可以工作,但是如果我同时运行这两个查询,我什么也得不到,因为如果可能的话,我似乎无法获得逻辑。

这是我迄今为止从 stackoverflow 得到的信息,但出现了错误。 希望有人可以重构它或使其发挥作用。

SELECT *,
(
SELECT COUNT(planters.todate), SUM(planters.todate)
FROM monthlyreport 
INNER JOIN planters ON monthlyreport.accountid = planters.accountid
WHERE monthlyreport.accountid = 190 AND MONTH(monthlyreport.compmonth) = MONTH(planters.todate)
GROUP BY monthlyreport.mthreportid, month(planters.todate)
) AS count_1,

(
SELECT COUNT(planters.compdate), SUM(planters.compdate)
FROM monthlyreport 
INNER JOIN planters ON monthlyreport.accountid = planters.accountid
WHERE monthlyreport.accountid = 190 AND MONTH(monthlyreport.compmonth) = MONTH(planters.compdate)
GROUP BY monthlyreport.mthreportid, month(planters.compdate)
) AS count_2

最佳答案

这不是很清楚,但据我所知,您想要的是在单个查询结果中获得两个结果。尝试根据两个表中的 accountID 将它们连接起来。AS:

SELECT *
from
(select accountID,COUNT(planters.todate) as count2date, SUM(planters.todate) as sum2date
-----
-----) count_1
inner join
(SELECT accountID,COUNT(planters.compdate) as countcomp, SUM(planters.compdate) as sumcomp
-----
-----) count_2
using(accountID);

请勿在 count_1 或 count_2 之前使用“AS”。最好将外部选择查询中的 * 替换为更具体的属性,例如 count_1.count2date 等。

希望这有帮助!如果您正在寻找其他内容,请告诉我。

-----更新-----

查看您上传的文件后,我提出了以下查询:

SELECT count1.compmonth, IFNULL( todatecount, 0 ) , IFNULL( todatesum, 0 ) , IFNULL(      compdatecount, 0 ) , IFNULL( compdatesum, 0 ) 
FROM count_1
LEFT JOIN count_2 ON count_1.compmonth = count_2.compmonth
UNION 
SELECT count2.compmonth, IFNULL( todatecount, 0 ) , IFNULL( todatesum, 0 ) , IFNULL( compdatecount, 0 ) , IFNULL( compdatesum, 0 ) 
FROM count_1
RIGHT JOIN count_2 ON count_1.compmonth = count_2.compmonth

您可以根据自己的意愿设置 0 的格式。另外,如果您的数据库平台支持“FULL OUTER JOIN”,您可以使用它来代替左连接和右连接的并集。

您必须将“FROM count_1”替换为:
FROM(选择 accountID,COUNT(planters.todate) 作为 count2date,SUM(planters.todate) 作为 sum2date ----- -----) count_1

FROM count_2类似。我知道这看起来像是一个巨大的查询,但它所做的只是在共同日期上连接两个表,并且所有其他不匹配的字段都指定为 NULL。

关于php - MYSQL 多重计数和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393823/

相关文章:

php - Azure 函数应用程序 - PHP $_GET/$_POST/$_REQUEST

php、mySQL 返回最后 x 行

带条件的 PHP MySQL 查询

php - 如何在单个查询中查询 3 个表?

mysql - 即使自动递增也出现 SQL 错误

php - Laravel @parent 不工作

javascript - 如何使我的动态下拉列表也依赖于父下拉列表?

java - 用按钮单击时的查询替换 jtable 中的先前行 - java swing

mysql - 如何使用 mysql 防止在 VARCHAR 列中使用数字?

php - Opencart - 基于名称和类型的自定义选项