-3

我有两张桌子。组 (Id,title,parentGroupID) 帐户 (AID,GroupID,Title,Balanace)。

团体

ID | Title | parentGroupID
1  | Assets| 0
2  | Bank  | 1
3  | myBank| 2
4  | cash  | 1

账户

AID | GroupID | Title        | Balanace
1   | 3       | acc1234      | 5000
2   | 3       | acc002       | 10000
3   | 4       | counter cash | 60000

现在我想显示资产 = 5000+10000+60000,因为它们都是资产组的成员。

4

1 回答 1

0

玩这个,看看你得到了什么:

SELECT
    t1.Title as Group1,
    t2.Title as Group2,
    t3.Title as Group3,
    t4.Title as Group4,
     t5.Title as Group5,
    a1.Title as SubGroup1,
    a2.Title as SubGroup2,
    a3.Title as SubGroup3,
    a4.Title as SubGroup4,
     a5.Title as SubGroup5,
    t1.parentGroupID,
    t2.parentGroupID,
    t3.parentGroupID,
    t4.parentGroupID,
    t5.parentGroupID,
    a1.Balance,
    a2.Balance,
    a3.Balance,
    a4.Balance,
     a5.Balance
           FROM GROUPS t1
      LEFT JOIN GROUPS t2 ON t2.parentGroupID = t1.ID
      LEFT JOIN GROUPS t3 ON t3.parentGroupID = t2.ID
      LEFT JOIN GROUPS t4 ON t4.parentGroupID = t3.ID
      LEFT JOIN GROUPS t5 ON t5.parentGroupID = t4.ID
      LEFT OUTER JOIN ACCOUNTS a1 ON a1.GroupID = t1.ID
      LEFT OUTER JOIN ACCOUNTS a2 ON a2.GroupID = t2.ID
      LEFT OUTER JOIN ACCOUNTS a3 ON a3.GroupID = t3.ID
      LEFT OUTER JOIN ACCOUNTS a4 ON a4.GroupID = t4.ID
      LEFT OUTER JOIN ACCOUNTS a5 on a5.GroupID = t5.ID
WHERE t1.parentGroupID = 0
于 2013-03-13T17:37:50.370 回答