Mysql 修改的前序树遍历子汇总

Mysql 修改的前序树遍历子汇总,mysql,mptt,Mysql,Mptt,我正在尝试按子公司汇总开支,包括所有没有任何递归函数的孙子公司 我的数据集与此格式类似: Parent A - Child A.1 - $1,000 - Child A.2 - $2,000 - - Grandchild A.2.1 - $500 - - Grandchild A.2.2 - $750 - Child A.3 - $3,000 - Child A.4 - $4,000 Parent B - Child B.1 - $11,000 - Child B.2 - $12

我正在尝试按子公司汇总开支,包括所有没有任何递归函数的孙子公司

我的数据集与此格式类似:

Parent A
 - Child A.1 - $1,000
 - Child A.2 - $2,000
 - - Grandchild A.2.1 - $500
 - - Grandchild A.2.2 - $750
 - Child A.3 - $3,000
 - Child A.4 - $4,000
Parent B
 - Child B.1 - $11,000
 - Child B.2 - $12,000
 - - Grandchild B.2.1 - $1,500
 - - Grandchild B.2.2 - $1,750
 - Child B.3 - $13,000
 - Child B.4 - $14,000
我要做的是对父级A按子级求和,因此输出结果如下:

Child A.1 - $1,000
Child A.2 - $3,250
Child A.3 - $3,000
Child A.4 - $4,000
这是“我的公司”表的简化结构:

id
name
parent_id
lft
rght
这是我的支出表的简化结构:

id
company_id
amount
date
我知道如何列出每个孩子以及他们的金额,仅针对父母A:

SELECT
`Company`.`name` AS `name`,
SUM(`Spend`.`amount`) AS `amount`
FROM
`spend_table` AS `Spend`
INNER JOIN companies_table AS `Company` ON `Spend`.`company_id` = `Company`.`id` 
INNER JOIN companies_table AS `thisCompany` ON `Company`.`lft` BETWEEN `thisCompany`.`lft` AND `thisCompany`.`rght`
WHERE
`thisCompany`.`name` = 'Parent A'
GROUP BY
`Company`.`name`
SELECT
`Company`.`name` AS `name`,
SUM(`Spend`.`amount`) AS `amount`
FROM
`spend_visibility2` AS `SpendVisibility`
`spend_table` AS `Spend`
INNER JOIN companies_table AS `Company` ON `Spend`.`company_id` = `Company`.`id` 
INNER JOIN companies_table AS `thisCompany` ON `Company`.`lft` BETWEEN `thisCompany`.`lft` AND `thisCompany`.`rght`
WHERE
`thisCompany`.`name` = 'Parent A'
`Company`.`parent_id` = `thisCompany`.`id`
GROUP BY
`Company`.`name`
这将产生:

Child A.1 - $1,000
Child A.2 - $2,000
Grandchild A.2.1 - $500
Grandchild A.2.2 - $750
Child A.3 - $3,000
Child A.4 - $4,000
Child A.1 - $1,000
Child A.2 - $2,000
Child A.3 - $3,000
Child A.4 - $4,000
我知道如何为父母A的每个孩子(不包括孙子女)求和:

SELECT
`Company`.`name` AS `name`,
SUM(`Spend`.`amount`) AS `amount`
FROM
`spend_table` AS `Spend`
INNER JOIN companies_table AS `Company` ON `Spend`.`company_id` = `Company`.`id` 
INNER JOIN companies_table AS `thisCompany` ON `Company`.`lft` BETWEEN `thisCompany`.`lft` AND `thisCompany`.`rght`
WHERE
`thisCompany`.`name` = 'Parent A'
GROUP BY
`Company`.`name`
SELECT
`Company`.`name` AS `name`,
SUM(`Spend`.`amount`) AS `amount`
FROM
`spend_visibility2` AS `SpendVisibility`
`spend_table` AS `Spend`
INNER JOIN companies_table AS `Company` ON `Spend`.`company_id` = `Company`.`id` 
INNER JOIN companies_table AS `thisCompany` ON `Company`.`lft` BETWEEN `thisCompany`.`lft` AND `thisCompany`.`rght`
WHERE
`thisCompany`.`name` = 'Parent A'
`Company`.`parent_id` = `thisCompany`.`id`
GROUP BY
`Company`.`name`
这将产生:

Child A.1 - $1,000
Child A.2 - $2,000
Grandchild A.2.1 - $500
Grandchild A.2.2 - $750
Child A.3 - $3,000
Child A.4 - $4,000
Child A.1 - $1,000
Child A.2 - $2,000
Child A.3 - $3,000
Child A.4 - $4,000

有人能帮我吗?我相信我需要一个子选择,但我很难搞清楚。首先,选择你感兴趣的子公司(表c)。我使用子查询轻松地选择“父a”上的直接子项。然后再次连接到companys_表以检索所有子体(表别名c2)。最后,加入您的支出表,以获得您可以使用group by聚合的金额

select c.name, sum(s.amount)
from companies_table c
    join companies_table c2 ON c2.lft between c.lft and c.rght
    join spend_table s ON c2.id = s.company_id
where parent_id = (select id from companies_table where name = 'Parent A')
group by c.name

首先,选择您感兴趣的子公司(表c)。我使用子查询轻松地选择“父a”上的直接子项。然后再次连接到companys_表以检索所有子体(表别名c2)。最后,加入您的支出表,以获得您可以使用group by聚合的金额

select c.name, sum(s.amount)
from companies_table c
    join companies_table c2 ON c2.lft between c.lft and c.rght
    join spend_table s ON c2.id = s.company_id
where parent_id = (select id from companies_table where name = 'Parent A')
group by c.name

完美的谢谢你的帮助!完美的谢谢你的帮助!