title: How to count multiple columns in a MySQL query
body: <p>I have two tables for saving user queries in my site. One for Tickets and the other for Ticket Replies. What I want is to count total tickets + total replies + total tickets with different status's. I can successfully count total tickets and total replies but I am having problem with counting ticket status's.</p>
  
  <p>The problem:
  When I use following query, it shows total new and open tickets but as the query is a JOIN query, it shows total status's of all rows (which means it's showing sum of status's for all replies and not all tickets). </p>
  
  <pre><code>SELECT  COUNT(tr.ticketid) totalReplies, 
      COUNT(DISTINCT(t.ticketid)) totalTickets,
          SUM(CASE WHEN t.status='Open' THEN 1 END) totalOpen,
          SUM(CASE WHEN t.status='Close' THEN 1 END) totalClose
  FROM `tbl_ticket` t 
  INNER JOIN `tbl_ticket_reply` tr ON tr.ticketid = t.ticketid
  </code></pre>
  
  <p>Hope I am making myself clear. I will explain it with an example. Let's say we have total following records:</p>
  
  <pre><code>total tickets: 10
  total replies: 20
  </code></pre>
  
  <p>Let's say, half tickets are opened and half closed. Then it should show 5 open, 5 closed. But instead, it gives 10 open and 10 closed.</p>
  
  <p>Update:
  I do not want to group my query by status as it would then distribute my total tickets among status's.</p>
  
  <p>Any help? Thanks</p>