2

我在尝试解决这个问题时遇到了一些麻烦。我有一个账户图表,我想总结一下钱栏。我的表“类别”有一个自动关系:

category_table
- id
- parent_id
- category

** 我不知道账户图表有多深。它可以有 1,2,3,4,5...多个级别。

这是一个例子:

我想要的是:

Category Name           |$
-------------------------------
Category 1              |160.00
    Category 1.1        |130.00
        some expense    | 50.00
        some expense    | 80.00
    Category 1.2        | 30.00
        some expense    | 10.00
        some expense    | 20.00
-------------------------------
Category 2              |160.00
    Category 2.1        |150.00
        some expense    |100.00
        some expense    |150.00
    Category 2.1        | 70.00
        some expense    | 50.00
        some expense    | 20.00
-------------------------------

我有的:

Category Name           |$
-------------------------------
Category 1              |  0.00
    Category 1.1        |  0.00
        some expense    | 50.00
        some expense    | 80.00
    Category 1.2        |  0.00
        some expense    | 10.00
        some expense    | 20.00
-------------------------------
Category 2              |  0.00
    Category 2.1        |  0.00
        some expense    |100.00
        some expense    |150.00
    Category 2.1        |  0.00
        some expense    | 50.00
        some expense    | 20.00
-------------------------------

我已经在php中尝试过这个递归函数,但它不起作用=/

function display_children($parent) {
    $sql = "SELECT c.id, c.category,
                   (SELECT COUNT(*) FROM category c1 WHERE c1.parent_id = c.id) As Total,
                   (SELECT SUM(p.value) FROM payables p WHERE p.category_id = c.id) As TotalToPay
            FROM category c
            WHERE c.parent_id = {$parent}
            ORDER BY c.category";
    $result = mysql_query($sql);
    $ret = "<ul>";
    $totalcategory = 0;
    while ($row = mysql_fetch_assoc($result)) {
        if ($row['Total'] > 0) {
            $totalcategory = $totalcategory + (empty($row['TotalToPay']) ? 0 : $row['TotalToPay']);
            $ret .= "<li><a href='#" . $row['id'] . "'>" . $row['category'] . " - " . $totalcategory . "</a>";
            $ret .= display_children($row['id']);
            $ret .= "</li>";
        } elseif ($row['Total']==0) {
            $totalcategory = ($totalcategory + empty($row['TotalToPay']) ? 0 : $row['TotalToPay']);
            $ret .= "<li><a href='" . $row['id'] . "'>" . $row['category'] . " - " . $totalcategory . "</a></li>";
        } else;
    }
    $ret .= "</ul>";
    return $ret;
}
echo display_children(0);

它向我展示了:

Category 1 - 0
    Category 1.1 - 0
    Category 1.2 - 0
        expense 1 - 25.90
        expense 2 - 0
        expense 3 - 3.80
Category 2 - 0
    Category 2.1 - 0
    Category 2.2 - 0
    Category 2.3 - 0
Category 3 - 0
    Category 3.1 - 52.00

...任何想法?...谢谢 :)

4

1 回答 1

0

如果你有一个像 3 这样的最大深度,你可以用一个这样的查询来做到这一点:

SELECT c1.dollar + c2.dollar2 + c3.dollar AS dollar_sum
FROM category_table c1
LEFT JOIN category_table c2 ON c1.id = c2.parent_id
LEFT JOIN category_table c3 ON c2.id = c3.parent_id

如果您不知道层次结构的最大深度,您确实可以像这样在 php 中使用 recusive 函数

function getSum($parent = null) {


    if ($parent === null) {

        $rows = // SELECT id, dollar FROM category_table WHERE parent_id IS NULL;

    } else {

        $rows = // SELECT id, dollar FROM category_table WHERE parent_id = $parent;
    }

    foreach ($rows as &$row) {

        $children = getSum($row->id);
        foreach ($children as $child) {

            $row->dollar += $child->dollar;
        }
    }

    return $rows;
}
于 2012-11-30T16:43:21.907 回答