title: optimise mysql query which is using two sub query and have left join for 3 tables need better solution
body: <p>I have a big query which is joining 3 tables and have two subqueries I want to optimise it. I was thinking to extract subqueries in to seprate query but for that I have to use an extra for loop and have to run query inside for loop for n number of time. Is there any better way to find data ? want to optimize my solution . Please provide me good solution/suggestion </p>
  
  <p>Below is my query.</p>
  
  <pre><code>    SELECT `e`.`id`
      ,(SELECT sum(sl.amount)
        FROM `sales` AS `sl`
        LEFT JOIN `trans` AS `t` ON t.id = sl.transId
        WHERE (sl.entity_id = e.id)
            AND (DATE_FORMAT(t.accounting_date, '%Y-%m')) = '2015-02') AS `2015-02`
      ,(SELECT sum(sl.amount)
        FROM `sales` AS `sl`
        LEFT JOIN `trans` AS `t` ON t.id = sl.transId
        WHERE (sl.entity_id = e.id)
            AND (DATE_FORMAT(t.accounting_date, '%Y-%m'))) AS `2015-01`
      ,`e`.`forenames`
      , `e`.`surname`
      , `e`.`company_name`
      , `e`.`address1`
      ,`e`.`address2`
      , `e`.`phone1`
      ,`e`.`postcode`
      , eassoc.associated_entity_id IS NULL AS `is_parent`
      ,`ent`.`accountPersonManager`
  FROM `entity` AS `e`
  LEFT JOIN `entity_link` AS `eassoc` ON eassoc.linkentity_id = e.id 
  LEFT JOIN `entities` AS `ent` ON ent.entity_id = e.id 
  WHERE (e.archived = 0
      OR e.archived IS NULL) 
  GROUP BY `e`.`id`
  ORDER BY `2015-02` DESC LIMIT 75
  </code></pre>