title: Change MySQL query from WHERE to LEFT JOIN
body: <p>I have a query which reports on Asterisk call usage and queue statistics. The query currently uses MySQL WHERE clauses to join the tables and filter the data.</p>
  
  <p>This is how the query looks at present:</p>
  
  <pre><code>SELECT 
      c.name as 'Agent',
      e.extended_number AS 'Extension',
      COUNT(ql.`time`) AS 'Total Inbound Calls',
      (SELECT 
              COUNT(1) 
      FROM
          call_history ch,
          ast_queue_mstatus qm
      WHERE
          ch.start &gt;= (DATE_SUB(DATE(NOW()),
              INTERVAL 10050 MINUTE))
              AND ch.start &lt;= (DATE(NOW()))
              AND ch.calltype = 'out'
              AND ch.flow = 'out'
              AND ch.extension_number = qm.membername
              AND qm.membername = e.extended_number
      GROUP BY qm.membername) AS 'Total Outbound Calls',
      FORMAT(SUM(CAST(ql.arg2 AS UNSIGNED)) / 60, 2) AS 'Total Inbound Duration',
      FORMAT(AVG(CAST(ql.arg2 AS UNSIGNED)) / 60, 2) AS 'Avg Inbound Duration',
      FORMAT(AVG(CAST(ql.arg1 AS UNSIGNED)) / 60, 2) AS 'Avg Caller Hold Time'
  FROM
      ast_queue_log ql,
      ast_queue_mstatus qs,
      client c,
      extension e
  WHERE
      ql.queuename = '1234'
      AND ql.`time` &gt;= (DATE_SUB(DATE(NOW()), INTERVAL 7 DAY))
      AND ql.`time` &lt;= (DATE(NOW()))
      AND (ql.event = 'COMPLETEAGENT'
      OR ql.event = 'COMPLETECALLER'
      OR ql.event = 'COMPLETETRANSFER')
      AND RIGHT(ql.agent, 8) = qs.membername
      AND qs.membername = e.extended_number
      AND e.client_id = c.id      
  GROUP BY ql.agent
  ORDER BY c.name;
  </code></pre>
  
  <p>The problem that I have is that I need to return outbound call results for queue members that have not received any inbound calls. At the moment if there are no records in <code>ast_queue_log</code> then the <code>WHERE</code> clause excludes the records, which means they do not get picked up by the scalar sub query.</p>
  
  <p>How can I change this query so I get all call records from <code>call_history</code> where there is a corresponding record in <code>ast_queue_mstatus</code>?</p>
  
  <p>Sorry if this is too complex or there is not enough info, I can try to simplify if this makes no sense!</p>