Tuesday, 9 April 2019

Using PHP MYSQLi how to get only debtors and only creditors managed with action_type DR and CR

I have created the balance sheet system using PHP MYSQLi. Where admin can manage clients account information. Now I want to get only DEBTORS and only CREDITORS. I am managing both entries using action_type field 'dr' or 'cr'.

Below is the table structure:

Table structure

I have tried the below query for getting only DEBTORS:

SELECT *, client_id
     , SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debits
     , SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) total_credits
     , SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debtors
     , SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) balance 
  FROM tbl_balancesheet 
 GROUP  
    BY client_id
HAVING balance <> 0

But it's getting all results with DEBTORS and CREDITORS.

How I can get only DEBTORS and CREDITORS using the query or through PHP code?

Also, how I can display dr and cr in the balance column?

Please help!!!

Thanks



from Using PHP MYSQLi how to get only debtors and only creditors managed with action_type DR and CR

No comments:

Post a Comment