title: Using PHP MYSQLi how to get only debtors and only creditors managed with action_type DR and CR
body: <p>I have created the balance sheet system using PHP MYSQLi. Where admin can manage clients account information. Now I want to get only <strong>DEBTORS</strong> and only <strong>CREDITORS</strong>. I am managing both entries using <code>action_type</code> field 'dr' or 'cr'.</p>
  
  <p>Below is the table structure:</p>
  
  <p><a href="https://i.stack.imgur.com/15Yna.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/15Yna.png" alt="Table structure"></a></p>
  
  <p>I have tried the below query for getting only DEBTORS:</p>
  
  <pre><code>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 &lt;&gt; 0
  </code></pre>
  
  <p>But it's getting all results with DEBTORS and CREDITORS.</p>
  
  <p>How I can get only DEBTORS and CREDITORS using the query or through PHP code? </p>
  
  <p>Also, how I can display dr and cr in the balance column?</p>
  
  <p>Please help!!!</p>
  
  <p>Thanks</p>