title: Add one more table to query and count the data SQL
body: <p>I try to select data from 4 table (last table need to count data)</p>
  
  <p>My MySQL tables structure</p>
  
  <p>users</p>
  
  <pre><code>id
  username
  </code></pre>
  
  <p>images</p>
  
  <pre><code>id
  user_id
  image
  </code></pre>
  
  <p>user_follow</p>
  
  <pre><code>id
  user_id
  follow_id
  </code></pre>
  
  <p>commentaries</p>
  
  <pre><code>id
  user_id
  image_id
  text
  </code></pre>
  
  <p>I have got this SQL query:</p>
  
  <pre><code>    $sql = "SELECT u.username as user, i.image as user_image, p.image, p.date
              FROM users u              
              LEFT JOIN user_follow f ON u.id = f.follow_id
              LEFT JOIN images p ON p.user_id = u.id
              LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
              WHERE f.user_id = 3 OR p.user_id = 3       
              ORDER BY p.date DESC";
  </code></pre>
  
  <p>This line return user current image (last image)</p>
  
  <pre><code> LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
  </code></pre>
  
  <p>it returns all images from me and my friends</p>
  
  <pre><code>[0] =&gt; Array
      (
          [user] =&gt; 8888
          [user_image] =&gt; second.jpg
          [image] =&gt; second.jpg
          [date] =&gt; 2012-01-24 14:42:27
      )
  
  [1] =&gt; Array
      (
          [user] =&gt; 8888
          [user_image] =&gt; second.jpg
          [image] =&gt; first.jpg
          [date] =&gt; 2012-01-24 14:42:27
      )
  
  [2] =&gt; Array
      (
          [user] =&gt; 3333
          [user_image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [date] =&gt; 2012-01-24 01:54:19
      )
  
  [3] =&gt; Array
      (
          [user] =&gt; 3333
          [user_image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [image] =&gt; aaaaaaaa.jpg
          [date] =&gt; 2012-01-24 01:49:57
      )
  </code></pre>
  
  <p>I tried to add </p>
  
  <pre><code> left join commentaries c ON c.user_id = u.id
  </code></pre>
  
  <p>and result was </p>
  
  <pre><code>[2] =&gt; Array
      (
          [user] =&gt; 3333
          [user_image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [date] =&gt; 2012-01-24 01:54:19
          [id] =&gt; 1
      )
  
  [3] =&gt; Array
      (
          [user] =&gt; 3333
          [user_image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [date] =&gt; 2012-01-24 01:54:19
          [id] =&gt; 2
      )
  
  [4] =&gt; Array
      (
          [user] =&gt; 3333
          [user_image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [image] =&gt; aaaaaaaa.jpg
          [date] =&gt; 2012-01-24 01:49:57
          [id] =&gt; 1
      )
  
  [5] =&gt; Array
      (
          [user] =&gt; 3333
          [user_image] =&gt; ax46l7v7vugnesk10whk_339.jpg
          [image] =&gt; aaaaaaaa.jpg
          [date] =&gt; 2012-01-24 01:49:57
          [id] =&gt; 2
      )
  </code></pre>
  
  <p>Duplicate user if it have commentaries (By the way [user] => 3333 have 2 comments in example)</p>
  
  <p>I am trying to add one more table "commentaries" and count how many commentaries have every picture (from me and my friends) if no commentaries with such $user_id then return 0</p>