title: PHP Mysql Left Join
body: <pre><code>&lt;?php
  $q = mysql_query("SELECT sub_cat.*, links.*
                   FROM links
                   LEFT JOIN sub_cat
                   ON links.p_id = sub_cat.id
                   WHERE sub_cat.p_id = '$id'
                   ORDER BY name ASC") or die (mysql_error());
  
  while ($r = mysql_fetch_assoc($q))
  {
      $links_name = $r['name'];
      $link_h3 = $links_name != '' ? '&lt;h3&gt;' . $links_name . '&lt;/h3&gt;' : '';
      //print $link_h3;
      print '&lt;pre&gt;';
      print_r ($r);
  }
  ?&gt;
  </code></pre>
  
  <p>I have two tables with rows like:</p>
  
  <h2>sub_cat</h2>
  
  <ul>
  <li>id</li>
  <li>name</li>
  <li>p_id</li>
  </ul>
  
  <h2>links</h2>
  
  <ul>
  <li>id</li>
  <li>links</li>
  <li>p_id</li>
  </ul>
  
  <p>In sub cat i have movie categories, like foreign language movies, national movies, uncategorised movies and so on. In links table i have concrete movie links and depending on sub category.</p>
  
  <p>The only thing is that i do not want dublicate titles (sub_cat.name).
  result is:</p>
  
  <blockquote>
    <p><strong>Without Category</strong> www.moviesite.com</p>
    
    <p><strong>Without Category</strong> www.moviesite2.com</p>
    
    <p><strong>Without Category</strong> www.moviesite3.com</p>
    
    <p><strong>Foreign Movies</strong> www.moviesite1.bla</p>
    
    <p><strong>Foreign Movies</strong> www.moviesite2.bla</p>
  </blockquote>
  
  <p><strong>I want to be</strong></p>
  
  <blockquote>
    <p><strong>Without Category</strong> www.moviesite.com</p>
    
    <p>www.moviesite2.com</p>
    
    <p>www.moviesite3.com</p>
    
    <p><strong>Foreign Movies</strong> www.moviesite1.bla</p>
    
    <p>www.moviesite2.bla</p>
  </blockquote>
  
  <p>and do not have any idea how to do this :(</p>
  
  <p>any help appreciated.</p>