0

我有以下三个表

  1. acc_chart_of_accounts
  2. acc_chart_of_sub_accounts
  3. acc_posting_details

所有用户都使用表 acc_chart_of_accounts 作为名义账户的起始列表,表 chart_of_sub_accounts 包含用户添加的任何新名义账户。表 acc_posting_details 包含账户交易条目。目标是使用 acc_chart_of_accounts 表作为基础显示名义账户列表,并将用户创建的任何其他名义代码与每个名义代码的总和添加到列表中,所有这些都由 company_id 过滤。为此,我尝试了以下方法:

我采用了 acc_chart_of_accounts 表和 acc_chart_of_sub_accounts 表,并将以下查询应用于下面的两个表。

acc_chart_of_accounts

名义上的acc_no coa_name


        10  Freehold property cost             
        20  Freehold property depreciation     
       110  Leasehold property cost            
       120  Leasehold property depreciation    
       210  Plant and machinery cost           

acc_chart_of_sub_accounts

名义上的acc_no coa_name


      1200  Bank current account  

这是从主表acc_chart_of_accounts和用户设置的子表acc_chart_of_sub_accounts中列出名义账户的查询...

SELECT nominal_acc_no,coa_name FROM
(
    SELECT nominal_acc_no ,coa_name FROM acc_chart_of_accounts
    UNION
    SELECT nominal_acc_no,coa_name FROM acc_chart_of_sub_accounts
) A;

这就是它给出的结果,我删除了一些结果以使事情更容易阅读,您可以看到该查询采用右表 acc_chart_of_sub_accounts 上的名义代码条目并将其插入到左表的列表中acc_chart_of_accounts 虽然在列表的末尾,但我想 ORDER BY 会对其进行排序。

因此,当用户将新的名义账户添加到他们的账户时,它会显示在主图表上,如下所示。

名义上的acc_no coa_name


        10  Freehold property cost             
        20  Freehold property depreciation     
       110  Leasehold property cost            
       120  Leasehold property depreciation    
       210  Plant and machinery cost                       
       **1200  Bank current account**    

我现在需要做的是将下表 acc_posting_details 添加到组合中,我需要能够将此表链接到其他两个表,以便它将任何总余额添加到返回的结果中。

post_detail_id 名义上的_acc_no 借记贷方 company_id


         1            2109    0.00   27.00             0
         2            2200    4.00    0.00             0
         3            7000   23.00    0.00             0
         4            2109    0.00   27.00             0
         5            2200    4.00    0.00             0
         6            6100   23.00    0.00             0

现在,当用户查看他们的会计科目表时,他们将看到主表中的所有名义代码加上他们可能设置的任何其他代码以及每个名义代码可能存在的任何余额。此查询必须通过过滤company_id 将来自 $_SESSION 值。

以下是我设法使用以下代码生成的查询的结果:

SELECT
    `acc_chart_of_accounts`.`nominal_acc_no`
    , `acc_chart_of_accounts`.`coa_name`
    , SUM(`acc_posting_details`.`debit`) AS `dabit`
    , SUM(`acc_posting_details`.`credit`) AS `credit`
    , `acc_posting_details`.`company_id`
FROM
    `acc_chart_of_accounts`
    LEFT JOIN `acc_posting_details` 
        ON (`acc_chart_of_accounts`.`nominal_acc_no` = `acc_posting_details`.`nominal_acc_no`)
GROUP BY `acc_chart_of_accounts`.`nominal_acc_no`;

nominal_acc_no coa_name 借方贷方


        10  Freehold property cost             (NULL)  (NULL)        
        20  Freehold property depreciation     (NULL)  (NULL)        
       110  Leasehold property cost            (NULL)  (NULL)        
       120  Leasehold property depreciation    (NULL)  (NULL)        
       210  Plant and machinery cost           (NULL)  (NULL)       
       310  Fixture and fittings cost          (NULL)  (NULL)        
       320  Fixture and fittings depreciation  (NULL)  (NULL)       
       410  Motor vehicles cost                (NULL)  (NULL)        
       420  Motor vehicles depreciation        (NULL)  (NULL)       
       700  Investments                        (NULL)  (NULL)        
       900  Goodwill                           (NULL)  (NULL)        
      1000  Stock                              (NULL)  (NULL)        
      1100  Trade debtors                      (NULL)  (NULL)     
      1103  Debtors and prepayments            (NULL)  (NULL)        
      2100  Purchase ledger control            (NULL)  (NULL)        
      2109  Creditors                            0.00   54.00           
      2200  VAT control acc                      8.00    0.00            
      2300  PAYE NI Liability                  (NULL)  (NULL)        
      2600  Bank loan                          (NULL)  (NULL)        
      2700  Hire purchase                      (NULL)  (NULL)       
      2800  Lease purchase                     (NULL)  (NULL)        
      2900  Other loans                        (NULL)  (NULL)        
      3000  Capital Acc BBF                    (NULL)  (NULL)        
      3100  Capital introduced                 (NULL)  (NULL)       
      3200  Profit and loss account            (NULL)  (NULL)        
      3300  Personal drawings                  (NULL)  (NULL)       
      4000  Sales                              (NULL)  (NULL)        
      4009  Discounts allowed                  (NULL)  (NULL)        
      4100  Eport sales                        (NULL)  (NULL)        
      4200  Royalties received                 (NULL)  (NULL)       
      4210  Commisions received                (NULL)  (NULL)        
      4220  Insurance payout received          (NULL)  (NULL)       
      4230  Rental income                      (NULL)  (NULL)        
      4240  Bank interest received             (NULL)  (NULL)        
      5000  Purchases                          (NULL)  (NULL)       
      5900  Opening stock                      (NULL)  (NULL)       
      5950  Closing stock                      (NULL)  (NULL)        
      6000  Direct labour                      (NULL)  (NULL)        
      6100  Goods out cost                      23.00    0.00           
      6200  Goods in cost                      (NULL)  (NULL)        

这只是读取两个帐户表,并没有读取 acc_chart_of_sub_accounts 中的额外名义帐户,但它给出了我想要结束的内容。我真的很难过,所以我真的可以做伸出援助之手。谢谢

大卫

4

1 回答 1

1

I think maybe you are looking for a sub select. Your question is kind of rambling, so I'm not sure.

SELECT
    A.`nominal_acc_no`
    , A.`coa_name`
    , SUM(`acc_posting_details`.`debit`) AS `dabit`
    , SUM(`acc_posting_details`.`credit`) AS `credit`
    , `acc_posting_details`.`company_id`
FROM
(
    SELECT nominal_acc_no ,coa_name FROM acc_chart_of_accounts
    UNION
    SELECT nominal_acc_no,coa_name FROM acc_chart_of_sub_accounts
) A
    LEFT JOIN `acc_posting_details` 
        ON (A.`nominal_acc_no` = `acc_posting_details`.`nominal_acc_no`)
GROUP BY A.`nominal_acc_no`;
于 2013-04-17T19:02:55.363 回答